C# 如何有条件地显示 ASP.NET MVC Razor 中的字段?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16814119/
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
How do I conditionally show a field in ASP.NET MVC Razor?
提问by dmikester1
I am very new to C# and ASP.NET MVC Razor. I want to show a field in my view if the field is not blank.
我对 C# 和 ASP.NET MVC Razor 很陌生。如果该字段不为空,我想在我的视图中显示该字段。
Code
代码
<tr class="hide" id="trPhone2">
<td class="editor-label">
@Html.LabelFor(model => model.phone2)
</td>
<td>
@Html.EditorFor(model => model.phone2)
</td>
<td>
@Html.ValidationMessageFor(model => model.phone2)
</td>
</tr>
Now, I want to output that first <tr>line if the model.phone2 is ""and else output:
现在,<tr>如果model.phone2 is ""and else 输出,我想输出第一行:
<tr id="trPhone2">
How do I do this using ASP.NET MVC Razor?
如何使用 ASP.NET MVC Razor 执行此操作?
采纳答案by gwin003
The syntax might not be perfect, but try this:
语法可能不完美,但试试这个:
@{
var trClass = string.IsNullOrEmpty(Model.phone2) ? "hide" : "";
}
<tr class="@trClass" id="trPhone2">
<td class="editor-label">
@Html.LabelFor(model => model.phone2)
</td>
<td>
@Html.EditorFor(model => model.phone2)
</td>
<td>
@Html.ValidationMessageFor(model => model.phone2)
</td>
</tr>
回答by Andrei
@if (string.IsNullOrEmpty(Model.phone2))
{
<tr class="hide" id="trPhone2">
}
else
{
<tr id="trPhone2">
}
回答by Murtuza Kabul
Simply wrap this field in if condition
只需将此字段包装在 if 条件中
@if (Model.phone2=="")
{
<tr class="hide" id="trPhone2">
}
else
{
<tr id="trPhone2">
}
<td class="editor-label">
@Html.LabelFor(model => model.phone2)
</td>
<td>
@Html.EditorFor(model => model.phone2)
</td>
<td>
@Html.ValidationMessageFor(model => model.phone2)
</td>
</tr>
alternatively, you can simply skip the entire rendering of field like this
或者,您可以像这样简单地跳过整个字段的渲染
@if (Model.phone2!="")
{
<tr id="trPhone2">
<td class="editor-label">
@Html.LabelFor(model => model.phone2)
</td>
<td>
@Html.EditorFor(model => model.phone2)
</td>
<td>
@Html.ValidationMessageFor(model => model.phone2)
</td>
</tr>
}
Which is a better approach as it removes the field entirely from the dom object so removes any possibility of being edited later.
这是一种更好的方法,因为它从 dom 对象中完全删除了该字段,因此消除了以后被编辑的任何可能性。
回答by SWeko
I would calculate the class name in a code block and output that. Something along the lines of:
我会在代码块中计算类名并输出。类似的东西:
@{
var phone2ClassName = string.IsNullOrWhiteSpace(Model.phone2) ? "hide" : string.Empty;
}
<tr class="@phone2ClassName" id="trPhone2">
...
回答by Ali Adravi
If it is very complicated logic then use like this:
如果它是非常复杂的逻辑,那么使用如下:
var trId = "";
if(Model[i].FeeType == (int)FeeTypeEnum.LateFee
|| Model[i].FeeType == (int)FeeTypeEnum.WaivedFee)
{
trId=String.Format("{0}_{1}", @Model[i].ProductId, @Model[i].FeeType);
}
else
{
trId = @Model[i].ProductId.ToString();
}
<tr id="@trId" >

