C# @Html.DisplayText 实际上不会显示文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14387396/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
@Html.DisplayText will not actually display text
提问by NealR
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?
以下是我的一个 ASP MVC3 索引页面上表格第一行中的第一部分。我在该页面加载时逐步执行了代码,可以看到条件的评估已正确完成,但不是“CE”或“PT”显示。我对 ASP MVC 很陌生,有人可以帮助我了解语法/解释发生了什么吗?
@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>
采纳答案by Dmitry Efimenko
use @:
or <text></text>
to specify html text inside a server side code if you do not have any other html in there.
如果您没有任何其他 html,请使用@:
或<text></text>
在服务器端代码中指定 html 文本。
if (item.TableName.Equals("AgentContEd"))
{
@:CE
}
else if (item.TableName.Equals("AgentProductTraining"))
{
<text>PT</text>
}
回答by Justin Bicknell
The DisplayText is synonomous for Model.PropertyName.. so Model.PropertyName = @Html.DisplayText('PropertyName')
DisplayText 是 Model.PropertyName 的同义词。所以 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:
因此,如果 CE 不是您模型的属性,并且您只是尝试输出原始文本,而不仅仅是用原始文本替换该语句:
if (item.TableName.Equals("AgentContEd"))
{
<text>CE</text>
}
回答by danmiser
You have to get Razor to realize that you are trying to display literal text. See this good
Razor syntax guidefor more information.
您必须让 Razor 意识到您正在尝试显示文字文本。有关更多信息,请参阅此优秀的
Razor 语法指南。
if (item.TableName.Equals("AgentContEd"))
{
<text>CE</text>
}
if (item.TableName.Equals("AgentContEd"))
{
<text>CE</text>
}
回答by vincent de g
There are like 5 different ways of displaying text. In order to display a string you need to use
有 5 种不同的文本显示方式。为了显示您需要使用的字符串
@Html.DisplayName(string)