2

我的网站中有 Locations 列。在数据库中有单独的表,

  1. 国家
  2. 状态
  3. 城市
  4. 地区

这些所有表 id 都存储到Locations表中,然后 Location Table Id 被称为CandidatepreferredLocation

我的问题是,我想显示存储的候选位置。

例如:

地点:国家,州,城市,地区。

在上面,如果 city 为 null 意味着它显示“对象引用未设置为对象”。

所以我使用如下代码,

仍然显示相同的错误。我不知道。有人建议我吗?

4

1 回答 1

1

. 这里有什么错误

在使用它们之前,您没有检查相应的属性是否不为空:

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

当然,为了避免在视图中进行这些检查,您可以确保在为该视图提供服务的控制器操作中正确初始化了这些属性。

于 2013-05-13T06:10:13.083 回答