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.
-
Simply wrap this field in if condition
@if (Model.phone2=="")
{
}
else
{
}
@Html.LabelFor(model => model.phone2)
@Html.EditorFor(model => model.phone2)
@Html.ValidationMessageFor(model => model.phone2)
alternatively, you can simply skip the entire rendering of field like this
@if (Model.phone2!="")
{
@Html.LabelFor(model => model.phone2)
@Html.EditorFor(model => model.phone2)
@Html.ValidationMessageFor(model => model.phone2)
}
Which is a better approach as it removes the field entirely from the dom object so removes any possibility of being edited later.
- 热议问题