我想按類別顯示產品列表。 我第一次登錄該網站時,會出現一個類別URL列表。如果您點擊一個網址,我會喜歡 以獲得與此類別相關的所有產品。 當我構建它時,我的HomeController基於類別。 當我爲瀏覽做視圖時,我選擇了模型的類別。我不確定它是否是好的。我也試圖使用它的產品,它也沒有工作。 有人可以解釋我應該做什麼做生成適當的解決方案? 感謝無法創建正確的視圖以顯示選定類別的產品URL
目前我得到這個錯誤: 傳遞到字典的模型項的類型爲「System.Collections.Generic.List`1 [MVCWebStore.Models.Produit]」,但本詞典需要的模型項目鍵入'MVCWebStore.Models.Categorie'。
這裏是HomeController.cs(建立在Produit基)
namespace MVCWebStore.Controllers
{
public class HomeController : Controller
{
private MVCWebStoreDdEntities1 db = new MVCWebStoreDdEntities1();
public ActionResult Browse(int id = 0)
{
var categorieProduitToEdit = (from c in db.Produits where c.IdCategorie == id select c);
if (categorieProduitToEdit == null)
{
return HttpNotFound();
}
return View(categorieProduitToEdit.ToList());
}
這裏是Browse.cshtml
@model MVCWebStore.Models.Categorie
@{
ViewBag.Title = "Browse";
}
<ul>
@foreach (var produit in Model.Produits)
{
<li>
@produit.Description
</li>
}
</ul>
Produit.cs
namespace MVCWebStore.Models
{
using System;
using System.Collections.Generic;
public partial class Produit
{
public Produit()
{
this.ItemPaniers = new HashSet<ItemPanier>();
}
public int IdProduit { get; set; }
public int IdCategorie { get; set; }
public string NomProduit { get; set; }
public string Description { get; set; }
public double Prix { get; set; }
public int Quantite { get; set; }
public virtual Categorie Categorie { get; set; }
public virtual ICollection<ItemPanier> ItemPaniers { get; set; }
}
}
Categorie.cs
namespace MVCWebStore.Models
{
using System;
using System.Collections.Generic;
public partial class Categorie
{
public Categorie()
{
this.Produits = new HashSet<Produit>();
}
public int IdCategorie { get; set; }
public string NomCategorie { get; set; }
public virtual ICollection<Produit> Produits { get; set; }
}
}
我做了你說的@model ICollection但又得到了另一個錯誤。這裏要點在產品表上並顯示選擇的類別。我不確定它應該是Categorie應該是產品insted?@model ICollection –
user3127986
這裏是錯誤消息CS1061:'System.Collections.Generic.ICollection'不包含'Produits'的定義並且沒有擴展方法'Produits'接受類型'System.Collections.Generic.ICollection '的第一個參數可以被找到(你是否缺少using指令或者程序集引用?) –
user3127986
我會更新我的回答 –