NoStressDeveloper
NoStressDeveloper

Reputation: 533

How to handle null values in mvc view?

<li>
   <%=Html.UITemplates().FieldLong().TextBox("OrderRequest.AdditionalParties[0].ContactDetails[0].Address.Line1", "Address", Model.OrderRequest.AdditionalParties[0].Address.Line1)%>
</li>

The above line when Address is null throws a null reference exception. I don't know how to handle it as I still want to display view while Address.Line1 should be displayed as empty string.

Upvotes: 0

Views: 3365

Answers (1)

vendettamit
vendettamit

Reputation: 14677

You can simply use the conditional operator to check the null values on any object. So in your case it would be:

Model.OrderRequest.AdditionalParties[0].Address == null ? string.Empty : Model.OrderRequest.AdditionalParties[0].Address.Line1 %>

Upvotes: 2

Related Questions