如何使用ASP.NET MVC和实体框架在详细信息视图中显示类别名称

如何使用ASP.NET MVC和实体框架在详细信息视图中显示类别名称,asp.net,asp.net-mvc,entity-framework,model-view-controller,Asp.net,Asp.net Mvc,Entity Framework,Model View Controller,项目类别类别: public class ItemCategory { [Key] public int CategoryId { get; set; } [Required] [Display(Name ="Category Name")] public string CategoryName { get; set; } public virtual ICollection<Ite

项目类别
类别:

   public class ItemCategory
   {
        [Key]
        public int CategoryId { get; set; }
        [Required]
        [Display(Name ="Category Name")]
        public string CategoryName { get; set; }
        public virtual ICollection<Item> Items { get; set; }
    }
这是来自items controller的代码:

   // GET: Items/Details/5
   public async Task<ActionResult> Details(int? id)
   {
      if (id == null)
      {
         return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
      }

      var items =  db.Items.Include(i => i.ItemCategory);

      if (items == null)
      {
          return HttpNotFound();
      }

      //var thisItem = db.Items.FirstOrDefault(item => item.ItemId == id);
      return View(await db.Items.FindAsync(id));
}
//GET:Items/Details/5
公共异步任务详细信息(int?id)
{
if(id==null)
{
返回新的HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
var items=db.items.Include(i=>i.itemcegory);
if(items==null)
{
返回HttpNotFound();
}
//var thisItem=db.Items.FirstOrDefault(item=>item.ItemId==id);
返回视图(wait db.Items.FindAsync(id));
}
查看代码如下:

   public class Item
   {
        [Key]
        public int ItemId { get; set; }
        [Required]
        [Display(Name = "Item Name")]
        public string ItemName { get; set; }
        [DataType(DataType.Currency)]
        public decimal Price { get; set; }
        [DataType(DataType.Text)]
        [Display(Name ="Category Code")]
        public int CategoryId { get; set; }

        public ItemCategory ItemCategory { get; set; }
        [NotMapped]
        public string CategoryName { get; set; }
   }
@model ContactManager.Models.Item
@{
    ViewBag.Title = "Details";
}
<h2>Details</h2>
<div>
    <h4>Item</h4>
    <hr />
    <dl class="dl-horizontal">
        <dt>
            @Html.DisplayNameFor(model => model.ItemCategory.CategoryName)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.ItemCategory.CategoryName)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.ItemCode)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.ItemCode)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.ItemName)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.ItemName)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.Price)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.Price)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.BarCode)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.BarCode)
        </dd>
    </dl>
</div>
<p>
    @Html.ActionLink("Edit", "Edit", new { id = Model.ItemId }) |
    @Html.ActionLink("Back to List", "Index")
</p>
@model ContactManager.Models.Item
@{
ViewBag.Title=“详细信息”;
}
细节
项目

@DisplayNameFor(model=>model.ItemCategory.CategoryName) @DisplayFor(model=>model.itemcegory.CategoryName) @DisplayNameFor(model=>model.ItemCode) @DisplayFor(model=>model.ItemCode) @DisplayNameFor(model=>model.ItemName) @DisplayFor(model=>model.ItemName) @DisplayNameFor(model=>model.Price) @DisplayFor(model=>model.Price) @Html.DisplayNameFor(model=>model.BarCode) @DisplayFor(model=>model.BarCode) @ActionLink(“编辑”,“编辑”,新的{id=Model.ItemId})| @ActionLink(“返回列表”、“索引”)


我不熟悉ASP.NETMVC和实体框架。当我执行代码时,视图不会在此视图中显示类别名称,我真的想知道如何在此视图中获取类别名称…谢谢

ItemCategory.CategoryName引用为空。更改您的项目型号。它会起作用的

  public class Item
    {
        public Item() // Note this
        {
            ItemCategory = new ItemCategory();
        }
        [Key]
        public int ItemId { get; set; }
        [Required]
        [Display(Name = "Item Name")]
        public string ItemName { get; set; }
        [DataType(DataType.Currency)]
        public decimal Price { get; set; }
        [DataType(DataType.Text)]
        [Display(Name = "Category Code")]
        public int CategoryId { get; set; }

        public ItemCategory ItemCategory { get; set; }
        [NotMapped]
        public string CategoryName { get; set; }
    }

检查“db.Items.FindAsync(id)”是否返回“ItemCategory”对象,然后检查“ItemCategory.CategoryName”中有什么值。是,它返回ItemCategory,但ItemCategory在视图中返回null值。