asp.net mvc - How to concisely create optional HTML attributes with razor view engine? -


I'm looking for a way to write the following code with a less written code (maybe 5) I think I selected I can do the same thing as a class but this razor syntax is not looking pretty.

  & lt; Ul & gt; @ Urach (in model. Main). {& Lt; Li @ (mk choose? Class = \ "selected \" "empty") gt; @if (string.IsNullOrEmpty (mi.Title)) {& lt; a href = "@ mi.Href"> @ Mi.Text & lt; / a & gt;} other {& lt; a href = "@ miles. HRFIF "title =" @metal "> gt; @ mi.Text & lt; / a & gt;} & lt; / li & gt;} & lt; / ul & gt;  

view

in ASP.NET MVC 4

Conditional Attribute Rendering

If you have a feature that can be tapped, then in the past you need to do a redundant check to avoid writing blank specialty, like this:

  & lt; div @ {if (myClass! = Null) {text }} & gt; Content & lt; / div & gt;  

Now the razor automatic If you are empty, then the attribute is not written:

  
"myClass" & gt; Content

So if @ my class is empty, then the output is just:

  
; Content & lt; / div & gt;

Comments