How do I conditionally show a field in ASP.NET MVC Razor?

落花浮王杯 提交于 2019-12-31 22:27:15

问题


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.

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 id="trPhone2">

How do I do this using ASP.NET MVC Razor?


回答1:


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>



回答2:


@if (string.IsNullOrEmpty(Model.phone2))
{
    <tr class="hide" id="trPhone2">
}
else
{
    <tr id="trPhone2">
}



回答3:


Simply wrap this field in if condition

@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.




回答4:


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">
...



回答5:


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


来源:https://stackoverflow.com/questions/16814119/how-do-i-conditionally-show-a-field-in-asp-net-mvc-razor

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