我对C#和ASP.NET MVC剃刀很新.如果该字段不为空,我想在我的视图中显示一个字段.
码
<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>
现在,我想输出第一个< tr>如果model.phone2为“”,则输出:
<tr id="trPhone2">
如何使用ASP.NET MVC Razor?
解决方法
语法可能不完美,但请尝试:
@{ 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>