Reputation: 1068
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"
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; }
}
}
Upvotes: 0
Views: 771
Reputation: 331
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)
Upvotes: 1