In all the Mvc Grid samples I have seen, the primary view is a list displayed in a grid . In my partucular case, the primary view displays a single record to be edited, Employee, and I need 2 or 3 paging Mvc Grids to show the employee's salary history, a list of employees reporting to him/her, etc.
Below is a simplistic prototype based on NWind Customer and Order tables using Entity Framework. When the user selects a different page in the Orders grid, the "Index(PagingParams args)" method is called. However, that method has no way to determine what the CustomerID is in order to retrieve the Order list for that customer again. (I have hard-coded the customerid to make the app compile). The only parameter passed is of PagingParams type, which does not have this information. If I could somehow pass a second parameter with the CustomerID I could accomplish what I need to.
Do you have an example of how to do this, when the main view is a single record, and I need to have a paging grid displaying some related detail? In my apps that is a fairly common paradigm for maintenance screens.
***** Customer view ********************
@model NWind.Customer
@{
Layout = null;
}
@using (Html.BeginForm("Edit", "Customer", new { @id = ViewBag.CustomerID }))
{
@Html.LabelFor(Model => Model.CustomerID) |
@Html.TextBoxFor(Model => Model.CustomerID) |
@Html.LabelFor(Model => Model.CompanyName) |
@Html.TextBoxFor(Model => Model.CompanyName) |
@{Html.RenderPartial( "CustomerOrders", ((List
)ViewData["Orders"]));}
}
***** CustomerOrders view ********************
@model List
@{Html.Grid("GridOrders")
.Datasource((List)ViewData["Orders"])
.Caption("Orders")
.EnablePaging()
.PageSettings(page =>
{
page.PageSize(10);
page.AllowPaging(true);
})
.Column(col =>
{
col.Add(a => a.OrderDate).HeaderText("Date");
col.Add(a => a.RequiredDate).HeaderText("Date Required");
col.Add(a => a.ShipCity).HeaderText("City");
col.Add(a => a.ShipCountry).HeaderText("Country");
})
.Height(300)
.AllowResizing(true)
.Render();
}
***** CustomerController ********************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NWind;
using System.Data;
using System.Data.Common;
using System.Data.Objects;
using System.Data.Linq;
using Syncfusion.Mvc.Grid;
namespace SyncfusionMvcApplication1.Controllers
{
public class CustomerController : Controller
{
nwindEntities1 _context = new nwindEntities1();
public ActionResult Index(string customerid)
{
Customer cust = _context.Customers.Where(a => a.CustomerID == customerid)
.FirstOrDefault();
ViewData.Model = cust;
ViewBag.CustomerID = cust.CustomerID;
List orders = _context.Orders.Where(a => a.CustomerID == customerid)
.DefaultIfEmpty().ToList();
ViewData["Orders"] = orders;
return View();
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(PagingParams args)
{
// *** Problem: The customer id needs to be passed here from the view when a new page is selected; hard-coded here so it will compile ***
string customerid = "ERNSH";
List orders = _context.Orders.Where(a => a.CustomerID == customerid)
.DefaultIfEmpty().ToList();
ActionResult actions = orders.GridActions();
return orders.GridActions();
}
}
}