html5 - showing error when value is null in mvc -


i have locations column in site. in database there separate tables ,

  1. country
  2. state
  3. city
  4. 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

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -