我的啤酒模型有啤酒厂对象,但是当我在视图中时,我无法访问啤酒厂的属性。 Intellisense似乎认为我可以访问brewery属性,但DisplayFor方法不会打印任何内容。
@model BeerRecommender.Models.Beer
@{
ViewBag.Title = "Details";
}
<h2>@Html.DisplayFor(model => model.Name)</h2>
<fieldset>
<legend>Details</legend>
<div class="display-label">Style: @Html.DisplayFor(model => model.Style.Name)</div>
<div class="display-label">Brewery: @Html.DisplayFor(model => model.Brewery.Name)</div>
</fieldset>
以下是Beer类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using BeerRecommender.Models.ViewModels;
namespace BeerRecommender.Models
{
public class Beer
{
[Key]
public int BeerID { get; set; }
public ICollection<City> Cities { get; set; }
public ICollection<Tag> Tags { get; set; }
public Style Style { get; set; }
public string Name { get; set; }
public Brewery Brewery { get; set; }
}
......这是Brewery类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.ComponentModel.DataAnnotations;
namespace BeerRecommender.Models
{
public class Brewery
{
[Key]
public int BreweryID { get; set; }
public string Name { get; set; }
public ICollection<Beer> Beers { get; set; }
public City City { get; set; }
}
}
答案 0 :(得分:2)
你是如何加载Beer类的?如果这是来自实体框架,则需要在加载代码中调用.Include(o =&gt; o.Brewery)以包含该表。假设是EF 4.1,否则你需要像.Include(&#34; Brewery&#34;)这样的字符串名称,如果在4.0中