简体   繁体   English

ASP.Net显示表,对于每个

[英]ASP.Net Displaying Table using For each

I apologies in advance for my formatting! 我事先为我的格式表示歉意!

I'm following a tutorial ( Here ) and I'm trying to create a details Page for Category but instead of displaying Course Title and Grade, I want to display the Product's id,Brand,Name,Barcode and Price of every product that has the same CategoryId. 我正在关注一个教程( Here ),并尝试为类别创建一个详细信息页面,但我不想显示课程标题和成绩,而是要显示具有以下内容的每个产品的产品ID,品牌,名称,条形码和价格相同的CategoryId。

However, I get this error: 但是,我收到此错误:

"Inventory.Models.Category does not contain a definiton for 'Category' and no extension method 'Category' accepting a first argument of type 'Invetory.Models.Category' could be found" “ Inventory.Models.Category不包含'Category'的定义,找不到扩展方法'Category'接受类型为'Invetory.Models.Category'的第一个参数”

在此处输入图片说明

在此处输入图片说明

Class Model 类模型

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

Category/Details.cshtml: Category / 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 产品.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 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; }
    }
}

Okay so you have quite a bit of problems but let's do this 好吧,您有很多问题,但是让我们这样做

1) your model is most likely Product not Category so change in your view @model Inventory.Models.Category to @model Inventory.Models.Product 1)您的模型很可能是产品而不是类别,因此在视图@model Inventory.Models.Category更改为@model Inventory.Models.Product

2) There is no Category in the Product Model so add that 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) The IEnumerable (ie the list you loop through) is called Categories not Category so change @foreach (var item in Model.Category) to @foreach (var item in Model.Categories) 3)IEnumerable(即您遍历的列表)称为类别而不是类别,因此将@foreach (var item in Model.Category)更改为@foreach (var item in Model.Category) @foreach (var item in Model.Categories)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM