【发布时间】:2015-11-09 11:28:41
【问题描述】:
我提前为我的格式道歉!
我正在学习一个教程 (Here),我正在尝试为类别创建一个详细信息页面,但我不想显示课程名称和等级,而是显示产品的 ID、品牌、名称、条形码和价格具有相同 CategoryId 的每个产品。
但是,我收到此错误:
“Inventory.Models.Category 不包含 'Category' 的定义,并且找不到接受类型为 'Invetory.Models.Category' 的第一个参数的扩展方法 'Category'”
类模型
public class Category {
public int Id{get;set;}
public string Name {get;set;}
}
public class Product{
public int Id{get;set;}
public int CategoryId{get;set;}
public String Brand {get;set;}
public String Name {get;set;}
public decimal Price{get;set;
}
类别/详细信息.cshtml:
@model Inventory.Models.Category
@{
ViewBag.Title = "Details";
}
<h2>Details</h2>
<div>
<h4>Category</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.Name) <--ERROR HERE
</dt>
<dd>
@Html.DisplayFor(model => model.Name) <--ERROR
</dd>
<!--Test start-->
<dt>
@Html.DisplayNameFor(model => model.Category)
</dt>
<dd>
<table class="table">
<tr>
<th>Category</th>
<th>ID</th>
</tr>
@foreach (var item in Model.Category)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Product.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.ID)
</td>
</tr>
}
</table>
</dd>
<!--test end-->
</dl>
</div>
<p>
@Html.ActionLink("Edit", "Edit", new { id = Model.ID }) |
@Html.ActionLink("Back to List", "Index")
</p>
产品.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Inventory.Models
{
public class Product
{
public int Id { get; set; }
public int CategoryId { get; set; }
public string Brand { get; set; }
public string Name { get; set; }
public string Barcode { get; set;}
public decimal Price { get; set; }
public virtual ICollection<Category> Categories { get; set; }
}
}
Category.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Inventory.Models
{
public class Category
{
public int ID { get; set; }
public string Name { get; set; }
public virtual Product Product { get; set; }
}
}
【问题讨论】:
标签: c# html .net asp.net-mvc