2015-08-20 24 views
1

我在MVC創建銀行應用程序由銀行銀行和負載錶行5避免重複上的ViewPage的結果MVC

負載表,

enter image description here

但我想不出做到這一點酷似上圖中,

這就是我得到,它有10個表,而不是僅僅2個表

enter image description here

這是上述觀點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 &nbsp /> @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'foreach'渲染模型中的所有項目。使用具有銀行屬性的視圖模型,幷包含與後者相關的數據集合。然後你的樣子是'foreach(模型中的var bank){//銀行名稱等foreach(銀行中的var詳細信息){//銀行記錄}}' –

+0

我確實像你說的那樣'foreach語句不能操作類型的變量'albaraka.Models.ProductApprovals',因爲'albaraka.Models.ProductApprovals'沒有包含'GetEnumerator''的公開定義,所以我應該怎麼做 – kez

+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

+0

我是否需要使用在控制器方法中傳遞數據的更改模型? – kez

+0

不知道我理解你的評論,但你可以保留你現有的數據模型(雖然屬性'SearchDate'似乎有點奇怪),並使用'.GroupBy()'按'SubsidaryName'對記錄進行分組。關鍵將是'SubsidaryName',並且這些值將是與該補貼相關聯的產品的列表。只是將值映射到您的視圖模型集合 –

+0

確實需要在控制器類中使用'BankVM'模型? – kez