Ext.NET MVC – Bind C# Class View Model to GridPanel

Ext.NET MVC – Bind C# Class View Model to GridPanel

 
   Class Model of Data Collection ( Present on GridPanel )
 
   public class Company
   {
     public int ID { get; set; }
     public string CompanyName { get; set; }
   }
 
   View Model of Page View
 
   public class CompanyDetailModel
   {
     … …
 
     public List<Company> companylist { get; set; }
   }
 
   Web Controller
 
   public ActionResult EntityFrameworkGrid()
   {
       CompanyDetail model = new CompanyDetail();
       List<Company> list = this.getAllCompanyItem();
 
       model.companylist = list;
 
       return this.View(model);
   }
 
   /*
      It would be called when the Page is changed from Grid Panel.
      Otherwise, the Grid Panel Proxy Function does not need to be defined from the Controller and the View.
      
      From the Paging Handling Function, you do not need to prepare the Paging Logic on it.
      You only need to pass the whole set of Records to the Store.
      ExtNet Library would handle the Implementation of Paging.

   */
   public ActionResult GetEntityFrameworkData()
   {
       List<Company> list = this.getAllCompanyItem();
 
       return this.Store(list);
   }
 
   // Get Record from Data Service Layer
   public List<Company> getAllCompanyItem()
   {
       … …
   }
 
   View
 
   @model ExtMVC.Controllers.CompanyDetail
 
   @(Html.X().GridPanel()
     .Title("Company")
     .ID("GridPanel")
     .Store(Html.X().Store()
       .Model(Html.X().Model())
         .Fields(   /* Define the Binding Attribute on the Grid Panel. */
           new ModelField("ID", ModelFieldType.Int),
           new ModelField("CompanyName", ModelFieldType.String)
         )
       )
       .DataSource(Model.companylist)   /* Assign the Collection Model Variable from Page View Model to Grid Panel. */
       .ServerProxy(   /* Define the Paging Handling ActionResult Function ( Controller Url Path ) from here. */
         Html.X().AjaxProxy()
           .Url(Url.Action("GetEntityFrameworkData"))
       )
     )
     .ColumnModel(   /* Define the Presentation Field from here. */
       Html.X().RowNumbererColumn(),
       Html.X().Column().Text("ID").DataIndex("ID").Width(75),
       Html.X().Column().Text("Company").DataIndex("CompanyName").Width(300)
     )
   )