html5 - showing error when value is null in mvc -
i have locations column in site. in database there separate tables ,
- country
- state
- city
- region
these table id's stored locations table location table id referred candidatepreferredlocation.
my problem is, want show location candidates stored.
eg:
location: country, state, city,region that.
in above, if city null means shows "object reference not set object."
so use code following,
still showing same error. don't know. 1 suggest me?
. mistake here
you didn't check whether corresponding properties not null before using them:
<% foreach (dial4jobz.models.candidatepreferredlocation cl in model.candidatepreferredlocations) { %> <% if (cl.location != null) { %> <% if (cl.location.country != null) { %> <%: cl.location.country.name %> <% } %> <% if (cl.location.state != null) { %> <%: cl.location.state.name %> <% } %> <% if (cl.location.city != null) { %> <%: cl.location.city.name %> <% } %> <% if (cl.location.region != null) { %> <%: cl.location.region.name %> <% } %> <% } %> <% } %>
of course in order avoid checks in view make sure properties correctly initialized in controller action serving view.
Comments
Post a Comment