@Html.EJS().Grid("Grid").DataSource(dataManager => { dataManager.Url("/Home/UrlDatasource").Adaptor("UrlAdaptor"); }).Columns(col =>
{
col.Field("AddressType").HeaderText("AddressType").Width("120").Add();
col.Field("AddressLine1").HeaderText("AddressLine1").Width("120").Add();
col.Field("Title").HeaderText("Title").Width("120").Add();
}).AllowPaging().AllowFiltering().Render() |
// you can bind the data that you want to show in Grid
public class CustomerAddressVM
{
public string AddressType { get; set; }
public string AddressLine1 { get; set; }
public string Title { get; set; }
} |
using EntityExampleWithRelationships.ViewModels;
. . . . .
public ActionResult UrlDatasource(DataManagerRequest dm) {
//Original entity collection
var customerAddresses = db.CustomerAddresses.Include(c => c.Address).Include(c => c.Customer);
//Viewmodel entity collection
List<CustomerAddressVM> customer = new List<CustomerAddressVM>();
foreach (var item in customerAddresses)
{
CustomerAddressVM objcvm = new CustomerAddressVM(); // ViewModel
objcvm.Title = item.Customer.Title;
objcvm.AddressLine1 = item.Address.AddressLine1;
objcvm.AddressType = item.AddressType;
customer.Add(objcvm);
}
return dm.RequiresCounts ? Json(new { result = customer.ToList(), count = count }) : Json(customer.ToList());
|
public ActionResult Update(CustomerAddressVM value)
{
var customer = db.CustomerAddresses.Where(c => c.CustomerID === customerModel.CustomerID);
customer.Customer.Title = customerModel.Title;
customer.Address.AddressLine1 = customerModel.AddressLine1;
customer.AddressType = customerModel.AddressType;
// Use the customer class to update the records
return Json(value, JsonRequestBehavior.AllowGet);
}
|