Avoid duplicating the results on viewpage in mvc

I'm creating banking application in MVC 5.

Load table by bank and Load table rows by Bank,

But I cannot figure out do this exactly like above image,

This what I'm getting , it has 10 tables, instead just 2 tables

this is cshtml code for above view

    @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>
}

This is Model class

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; }

    }
}

This is Controller class

 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);
}

How to stop this repeating ,appricate if can show a way to this in view page

Answers


You need a view model(s) to represent what you want to display in the view. It should be something like

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; }
}

and pass a collection of BankVM to the view

@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>
}

In the controller, you can use a Linq .GroupBy() method to populate your collection of BankVM


Need Your Help

Separating reserved identifiers in flex

bison lex flex-lexer

I have a language I am making a parser for which contains function calls. A few function names are reserved and I would like to handle them differently in my grammer. In EBNF it would look like

SQL calculations from different table

sql sql-server database ms-access

How i can perform calculations between different tables.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.