Clean way to output values in ASP.NET MVC Views when value is not null

Posted by Swoop on Stack Overflow See other posts from Stack Overflow or by Swoop
Published on 2010-05-21T20:31:01Z Indexed on 2010/05/21 20:50 UTC
Read the original article Hit count: 239

Filed under:
|

Is there a better way to write the code below? I have quite a few blocks that are similar, and this is making the code in the Viewpage very messy to work with.

The data value with the associated label only needs to be output when certain conditions are met, which is almost always if the value is not null.

The options I can think is to use a response.write to atleast minimize the usage of the ASP script tags, or to format the webpage is such a way that the label displays with an appropriate n/a type value.

<% if (myData.Balance != null)
{ %>                       
   Balance: <%= String.Format("{0:C}", (myData.Balance))%>                        
<% } %>

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about ASP.NET