NealR
NealR

Reputation: 10709

@Html.DisplayText will not actually display text

The following is the first section in the first row of a table on one of my ASP MVC3 Index pages. I've stepped through the code when that page loads, and can see that the evaluation of the conditions is done properly, however not of the "CE" or "PT" displays. I'm pretty new to ASP MVC, can someone help me with the syntax/explain what's going on?

@foreach (var item in Model.Where(i => i.Status != "C")) {
var Id = item.Id;
<tr>
    <td>
    @if (!String.IsNullOrWhiteSpace(item.TableName))
    {
        if (item.TableName.Equals("AgentContEd"))
        {
            @Html.DisplayText("CE");
        }
        else if (item.TableName.Equals("AgentProductTraining"))
        {
            @Html.DisplayText("PT");
        }
        else
        {
            @Html.DisplayFor(modelItem => item.TableName)
        }             
    }           
    </td>

Upvotes: 26

Views: 71235

Answers (4)

Justin Bicknell
Justin Bicknell

Reputation: 4808

The DisplayText is synonymous with Model.PropertyName, so Model.PropertyName = @Html.DisplayText('PropertyName')

So if CE is not an attribute of your model, and you are just trying to output raw text than just replace that statement with the raw text:

        if (item.TableName.Equals("AgentContEd"))
        {
            <text>CE</text>
        }

Upvotes: 14

vincent de g
vincent de g

Reputation: 289

There are like 5 different ways of displaying text. In order to display a string you need to use

@Html.DisplayName(string)

Upvotes: 19

danmiser
danmiser

Reputation: 1083

You have to get Razor to realize that you are trying to display literal text. See this good
Razor syntax guide for more information.

if (item.TableName.Equals("AgentContEd")) { <text>CE</text> }

Upvotes: 2

Dmitry Efimenko
Dmitry Efimenko

Reputation: 11188

use @: or <text></text> to specify html text inside a server side code if you do not have any other html in there.

if (item.TableName.Equals("AgentContEd"))
{
    @:CE
}
else if (item.TableName.Equals("AgentProductTraining"))
{
    <text>PT</text>
}

Upvotes: 35

Related Questions