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

后端 未结 5 1054
逝去的感伤
逝去的感伤 2021-02-05 17:11

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



        
5条回答
  •  无人及你
    2021-02-05 17:48

    The syntax might not be perfect, but try this:

        @{ 
            var trClass = string.IsNullOrEmpty(Model.phone2) ? "hide" : ""; 
        }
    
        
            
                @Html.LabelFor(model => model.phone2)
            
            
                @Html.EditorFor(model => model.phone2)
            
            
                @Html.ValidationMessageFor(model => model.phone2)
            
        
    

提交回复
热议问题