2013-05-13 32 views
2

我的站點中有Locations列。在數據庫中有單獨的表,當mvc中的值爲空時顯示錯誤

  1. 國家
  2. 國家
  3. 城市
  4. 地區

的存儲到位置這些都表ID表然後位置表ID稱爲到CandidatepreferredLocation

我的問題是,我想顯示候選人存儲的位置。例如:

例如:

位置:國家,州,市,這樣的地區。

在上面,如果city爲null意味着它顯示「未設置對象的對象引用」。

於是我就用這樣的代碼下面,

仍表現出同樣的錯誤。我不知道。任何人都建議我?

回答

1

。什麼是這裏

錯誤您並沒有勾選相應的屬性是否不爲null,使用前:

<% 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 %> 
     <% } %> 
    <% } %> 
<% } %> 

當然,爲了避免在視圖中這些檢查,你可以確保這些屬性在爲此視圖提供服務的控制器操作中正確初始化。

+0

謝謝。它運作良好。 – PoliDev 2013-05-13 06:25:05

相關問題