Clean way to output values in ASP.NET MVC Views when value is not null
- by Swoop
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))%>
<% } %>