C# MVC视图中的Foreach中的Foreach
大编辑:我已经编辑了我的完整帖子,我在冯V和约翰尼斯的帮助下得出了答案,非常感谢大家 我一直在尝试在索引视图中的foreach循环中进行foreach循环,以便在手风琴中显示我的产品。让我告诉你我是如何做到这一点的 以下是我的模型:C# MVC视图中的Foreach中的Foreach,c#,asp.net,asp.net-mvc,entity-framework,asp.net-mvc-4,C#,Asp.net,Asp.net Mvc,Entity Framework,Asp.net Mvc 4,大编辑:我已经编辑了我的完整帖子,我在冯V和约翰尼斯的帮助下得出了答案,非常感谢大家 我一直在尝试在索引视图中的foreach循环中进行foreach循环,以便在手风琴中显示我的产品。让我告诉你我是如何做到这一点的 以下是我的模型: 公共类产品 { [关键] 公共int ID{get;set;} public int CategoryID{get;set;} 公共字符串标题{get;set;} 公共字符串说明{get;set;} 公共字符串路径{get;set;} 公共虚拟类别{get;set;
公共类产品
{
[关键]
公共int ID{get;set;}
public int CategoryID{get;set;}
公共字符串标题{get;set;}
公共字符串说明{get;set;}
公共字符串路径{get;set;}
公共虚拟类别{get;set;}
}
公共类类别
{
[关键]
public int CategoryID{get;set;}
公共字符串名称{get;set;}
公共虚拟ICollection产品{get;set;}
}
这是一种一对多的关系,一个产品只有一个类别,但一个类别有许多产品
在我看来,以下是我想做的:
@model IEnumerable
@foreach(ViewBag.Categories中的变量类别)
{
@类别.名称
@foreach(模型中的var乘积)
{
if(product.CategoryID==category.CategoryID)
{
@产品名称
@if(System.Web.Security.UrlAuthorizationModule.CheckUrlAccessForPrincipal(“/admin”,用户,“GET”))
{
@Html.Raw(“-”)
@ActionLink(“编辑”,“编辑”,新{id=product.id},新{style=“背景色:黑色;颜色:白色!重要;”})
}
@产品说明
}
}
}
我不确定这是不是正确的方法,但这正是我正在尝试的。对于每个类别,将该类别的所有产品放在手风琴选项卡中
- 第一类
- 产品1
- 产品3
- 第2类
- 产品2
- 产品4
- 第3类
- 产品5
public ActionResult Index()
{
ViewBag.Categories=db.Category.OrderBy(c=>c.Name.ToList();
返回视图(db.Product.Include(c=>c.Category.ToList());
}
大编辑:我已经编辑了我的完整帖子,我在冯V和约翰尼斯的帮助下得出了答案,非常感谢大家 试试这个:
看起来您每次都在为每个产品循环,现在这是为每个与当前循环类别具有相同类别ID的产品循环
<div id="accordion1" style="text-align:justify">
@using (Html.BeginForm())
{
foreach (var category in Model.Categories)
{
<h3><u>@category.Name</u></h3>
<div>
<ul>
@foreach (var product in Model.Product.Where(m=> m.CategoryID= category.CategoryID)
{
<li>
@product.Title
@if (System.Web.Security.UrlAuthorizationModule.CheckUrlAccessForPrincipal("/admin", User, "GET"))
{
@Html.Raw(" - ")
@Html.ActionLink("Edit", "Edit", new { id = product.ID })
}
<ul>
<li>
@product.Description
</li>
</ul>
</li>
}
</ul>
</div>
}
}
@使用(Html.BeginForm())
{
foreach(Model.Categories中的var类别)
{
@类别.名称
@foreach(Model.product.Where(m=>m.CategoryID=category.CategoryID)中的var乘积
{
-
@产品名称
@if(System.Web.Security.UrlAuthorizationModule.CheckUrlAccessForPrincipal(“/admin”,用户,“GET”))
{
@Html.Raw(“-”)
@ActionLink(“编辑”,“编辑”,新的{id=product.id})
}
-
@产品说明
}
}
}
您有:
foreach (var category in Model.Categories)
然后
@foreach (var product in Model)
基于该视图和模型,似乎model
属于Product
类型,如果是,则第二个foreach
无效。实际上,如果返回Product
集合,第一个可能无效
更新:
public ActionResult Index()
{
//you don't need to include the category bc it does it by itself
//var model = db.Product.Include(c => c.Category).ToList()
ViewBag.Categories = db.Category.OrderBy(c => c.Name).ToList();
var model = db.Product.ToList()
return View(model);
}
你说得对,我正在退回型号为Product的产品。而且,我也退回
既然你已经指出了问题所在,那你就明白了。我怎么样
如果我不能这样做,我应该做我想做的事情吗
当您说要返回产品
类型的模型时,我很惊讶您的代码能够编译。以下是您的方法:
@foreach (var category in Model)
{
<h3><u>@category.Name</u></h3>
<div>
<ul>
@foreach (var product in category.Products)
{
<li>
put the rest of your code
</li>
}
</ul>
</div>
}
假设控制器的动作方法如下所示:
public ActionResult AllCategories(int id = 0)
{
return View(db.Categories.Include(p => p.Products).ToList());
}
public class Product
{
[Key]
public int ID { get; set; }
public int CategoryID { get; set; }
//new code
public virtual Category Category { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string Path { get; set; }
//remove code below
//public virtual ICollection<Category> Categories { get; set; }
}
public class Category
{
[Key]
public int CategoryID { get; set; }
public string Name { get; set; }
//new code
public virtual ICollection<Product> Products{ get; set; }
}
将模型修改为如下所示:
public ActionResult AllCategories(int id = 0)
{
return View(db.Categories.Include(p => p.Products).ToList());
}
public class Product
{
[Key]
public int ID { get; set; }
public int CategoryID { get; set; }
//new code
public virtual Category Category { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string Path { get; set; }
//remove code below
//public virtual ICollection<Category> Categories { get; set; }
}
public class Category
{
[Key]
public int CategoryID { get; set; }
public string Name { get; set; }
//new code
public virtual ICollection<Product> Products{ get; set; }
}
公共类产品
{
[关键]
公共int ID{get;set;}
public int CategoryID{get;set;}
//新代码
公共虚拟类别{get;set;}
公共字符串标题{get;set;}
公共字符串说明{get;set;}
公共字符串路径{get;set;}
//删除下面的代码
//公共虚拟ICollection类别{get;set;}
}
公共类类别
{
[关键]
public int CategoryID{get;set;}
公共字符串名称{get;set;}
//新代码
公共虚拟ICollection产品{get;set;}
}
那么你的控制器现在开始
@foreach (var category in ViewBag.Categories)
{
<h3><u>@category.Name</u></h3>
<div>
@foreach (var product in Model.where(p=>p.CategoryID == category.CategoryID))
{
<table cellpadding="5" cellspacing"5" style="border:1px solid black; width:100%;background-color:White;">
<thead>
<tr>
<th style="background-color:black; color:white;">
@product.Title
@if (System.Web.Security.UrlAuthorizationModule.CheckUrlAccessForPrincipal("/admin", User, "GET"))
{
@Html.Raw(" - ")
@Html.ActionLink("Edit", "Edit", new { id = product.ID }, new { style = "background-color:black; color:white !important;" })
}
</th>
</tr>
</thead>
<tbody>
<tr>
<td style="background-color:White;">
@product.Description
</td>
</tr>
</tbody>
</table>
}
</div>
}