0

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.

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"

enter image description here

enter image description here

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:

@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 Answer 1

1

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

2) There is no Category in the Product Model so add that

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 (i.e. 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)

Sign up to request clarification or add additional context in comments.

6 Comments

I actually have both Category/Details.cshtml and Product/Details.cshtml. I want lots of Products assigned to some categories. Eg Coca Cola has category Id 1 and category id 1 relates to ID in Category which is Drinks. I actually want Products to be displayed in my Category html file. So when I press Details in the Category.html it'll display a list of all the products assigned to that categoryID.
@JasonWan so the relationship between Category to Product is one to many or many to many?
Many to many! So For example We have in Product Coke with Category Id 1 which relates to Drinks in Category with is Id also 1
Okay so what I think you should do is: 1) Firstly make your models in a way that they support what you want i.e. both Category and Product model should have an IEnumerable collection of what they are referencing and this needs to be populated correctly. 2) Check every page that you are calling things that exists in that model i.e. if you write model.X than the model should have property with a public get
If you look back up ! After applying what you told me to do gives me that error.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.