在MVC视图中的Foreach
本文关键字:Foreach 视图 MVC | 更新日期: 2023-09-27 18:08:46
大编辑:我在Von V和Johannes的帮助下编辑了我的完整帖子的答案,非常感谢你们!!!!
我一直试图做一个foreach循环内foreach循环在我的索引视图显示我的产品在手风琴。我来告诉你我是怎么做的。
这些是我的模型:
public class Product
{
[Key]
public int ID { get; set; }
public int CategoryID { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string Path { get; set; }
public virtual Category Category { get; set; }
}
public class Category
{
[Key]
public int CategoryID { get; set; }
public string Name { get; set; }
public virtual ICollection<Product> Products { get; set; }
}
这是一个 1 - 1 1 -多关系,一个产品只有一个类别,而一个类别有许多产品。
下面是我在我的视图中要做的:
@model IEnumerable<MyPersonalProject.Models.Product>
<div id="accordion1" style="text-align:justify">
@foreach (var category in ViewBag.Categories)
{
<h3><u>@category.Name</u></h3>
<div>
@foreach (var product in Model)
{
if (product.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>
}
</div>
我不太确定这是正确的方法,但这就是我想做的。对于每个类别,将该类别的所有产品放在一个手风琴选项卡中。
- 类别1
- 产品3
- 类别2
- 产品2
- 产品4
- 三级 产品5
- 产品1
这里我将添加我的映射我的one-one one-many(谢谢Brian p)关系:
public class MyPersonalProjectContext : DbContext
{
public DbSet<Product> Product { get; set; }
public DbSet<Category> Category { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Entity<Product>();
modelBuilder.Entity<Category>();
}
}
我还要添加我的控制器,这样你就可以看到我是怎么做的了:
public ActionResult Index()
{
ViewBag.Categories = db.Category.OrderBy(c => c.Name).ToList();
return View(db.Product.Include(c => c.Category).ToList());
}
大编辑:我已经编辑了我的完整帖子的答案,我想出了冯V和约翰内斯的帮助,一个大谢谢你的家伙!!!!
假设你的控制器的动作方法是这样的:
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; }
}
然后你的since now控制器接受一个类别作为模型(而不是产品):
foreach (var category in Model)
{
<h3><u>@category.Name</u></h3>
<div>
<ul>
@foreach (var product in Model.Products)
{
// cut for brevity, need to add back more code from original
<li>@product.Title</li>
}
</ul>
</div>
}
更新:添加ToList()到控制器返回语句
你有:
foreach (var category in Model.Categories)
然后
@foreach (var product in Model)
根据该视图和模型,Model
似乎属于Product
类型,如果是,则第二个foreach
无效。实际上,如果您返回Product
的集合,第一个可能是无效的。
更新:
你是对的,我要退回产品类型的型号。我也是既然你已经指出了问题所在,你就应该明白是哪里出了问题。我怎么样?如果我不能这样做我就该做我想做的事
当你说你返回一个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>
}
这表明,而不是返回Product
,您返回Category
与Products的集合。类似于EF:
// I am typing it here directly
// so I'm not sure if this is the correct syntax.
// I assume you know how to do this,
// anyway this should give you an idea.
context.Categories.Include(o=>o.Product)
试试这个:
看起来每次都是针对每个产品进行循环,现在这里是针对与当前循环的类别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>
}
}
控制器
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);
}
视图
您需要使用给定的类别
like:=> Model.where(p=>p。CategoryID == CategoryID)
试试这个…
@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>
}