ASP.Net显示表使用每个

时间:2015-11-09 11:28:41

标签: c# html .net asp.net-mvc

我提前为我的格式化道歉!

我正在关注教程(Here)并且我正在尝试为类别创建详细信息页面,但不是显示课程标题和成绩,而是要显示产品的ID,品牌,名称,条形码和具有相同CategoryId的每种产品的价格。

然而,我收到此错误:

“Inventory.Models.Category不包含'类别'的定义,并且没有扩展方法'类别'接受类型'Invetory.Models.Category'的第一个参数可以找到”

enter image description here

enter image description here

班级模型

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

类别/ Details.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>

Product.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; }
    }
}

1 个答案:

答案 0 :(得分:1)

好的,所以你有很多问题,但让我们这样做

1)您的模型很可能是产品而不是类别,因此您的视图@model Inventory.Models.Category更改为@model Inventory.Models.Product

2)产品型号中没有类别,所以添加

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 Category Category { get; set; } // <--- Add this

    public virtual ICollection<Category> Categories { get; set; }
}

3)IEnumerable(即你循环的列表)被称为Categories而不是Category,所以将@foreach (var item in Model.Category)更改为@foreach (var item in Model.Categories)