我在MVC 5中创建银行应用程序。
按银行加载表格,按银行加载表格行,
但我无法弄清楚这与上面的图像完全相同,
这就是我得到的,它有10个表,而只有2个表
这是上面视图的cshtml代码
@model IEnumerable<albaraka.Models.ProductApprovals>
@{
ViewBag.Title = "Product_Approvals";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>sadfasf</h2>
<h2>Product Approvals</h2>
@using (Html.BeginForm("Index", "Student", FormMethod.Get))
{
<div> Filter by : Date @Html.TextBox("SearchString", ViewBag.CurrentFilter as string) Filter by : Country @Html.TextBox("SearchString", ViewBag.CurrentFilter as string) <input type="submit" value="Search" /> </div>
}
@foreach (var item in Model)
{
@Html.DisplayNameFor(model => model.SubsidaryName); <p   /> @Html.DisplayFor(modelItem => item.SubsidaryName);
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.ProductNameEn)
</th>
<th>
@Html.DisplayNameFor(model => model.ProdcutNameAr)
</th>
<th>
@Html.DisplayNameFor(model => model.CurrentStatus)
</th>
<th>
Actions
</th>
</tr>
@foreach (var inside in Model)
{
if (inside.Subsidary_ID == item.Subsidary_ID)
{
<tr>
<td>
@Html.DisplayFor(modelItem => inside.ProductNameEn)
</td>
<td>
@Html.DisplayFor(modelItem => inside.ProdcutNameAr)
</td>
<td>
@Html.DisplayFor(modelItem => inside.CurrentStatus)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
</td>
</tr>
}
}
</table>
}
这是Model类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace albaraka.Models
{
public class ProductApprovals
{
public DateTime SearchDate { get; set; }
public string Country { get; set; }
public string Product_ID { get; set; }
public string Subsidary_ID { get; set; }
public string SubsidaryName { get; set; }
public string ProductNameEn { get; set; }
public string ProdcutNameAr { get; set; }
public string CurrentStatus { get; set; }
}
}
这是Controller类
public ActionResult Product_Approvals()
{
var productApproveResult =(from p in db.AB_Product
join s in db.AB_Subsidary on p.Subsidary_ID equals s.SubsidaryID
select new ProductApprovals
{
Product_ID = p.ProductID,
Subsidary_ID = s.SubsidaryID,
SubsidaryName = s.SubsidaryNameEn,
ProductNameEn = p.ProductTitleEn,
ProdcutNameAr = p.ProductTitleAr,
CurrentStatus = p.Status
}).ToList();
return View(productApproveResult);
}
如果可以在视图页面中显示这种方法,可以停止这种重复,appricate
答案 0 :(得分:2)
您需要一个视图模型来表示要在视图中显示的内容。它应该像
public class BankVM
{
public string SubsidaryName { get; set; }
public IEnumerable<ProductVM> Products { get; set; }
}
public class ProductVM
{
public string ProductNameEn { get; set; }
public string ProdcutNameAr { get; set; }
public string CurrentStatus { get; set; }
}
并将BankVM
的集合传递给视图
@model IEnumerable<yourAssembly.BankVM>
@foreach(var bank in Model)
{
<h3>@Html.DisplayFor(m => bank.SubsidaryName)</h3>
<table>
foreach(var product in bank.Products)
{
<tr>
<td>@Html.DisplayFor(m => product.ProductNameEn)</td>
<td>@Html.DisplayFor(m => product.ProductNameAr)</td>
....
</tr>
}
</table>
}
在控制器中,您可以使用Linq .GroupBy()
方法填充BankVM