@Html.DisplayText will not actually display text

折月煮酒 提交于 2019-12-30 05:45:07

问题


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>

回答1:


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>
}



回答2:


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

@Html.DisplayName(string)



回答3:


The DisplayText is synonomous for 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>
        }



回答4:


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> }



来源:https://stackoverflow.com/questions/14387396/html-displaytext-will-not-actually-display-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!