14

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?

0

5 Answers 5

10

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>
Sign up to request clarification or add additional context in comments.

1 Comment

works like a charm! You can also do it like @Html.ActionLink(" ", "action", "controller", new { id = id }, new { @class = "btn btn-default btn_details " + @trClass })
8
@if (string.IsNullOrEmpty(Model.phone2))
{
    <tr class="hide" id="trPhone2">
}
else
{
    <tr id="trPhone2">
}

6 Comments

This looked nice and simple but it was throwing errors in my code. I think because I was mixing c# in with html.
@dmikester1, show us your code, I believe the error can be easily fixed
I ended up using one of the other answers. Thanks
I pasted your code right in between my other HTML and it was saying there were two id's with trPhone2 and there was 2 open < tr > blocks.
@dmikester1, you should have replaced the first line with the snippet above. Otherwise the snippet was inserting one <tr> tag and the first line of our code was inserting another one - seems like that was the problem. Anyway, its good to know you have found a solution.
|
2

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.

Comments

1

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.