How to bind MVC grid to a specific list property in my model

I can bind my grid to the whole model, here's an example of one working grid:

    @(Html.EJ().Grid<API_EventListDTO>("EventListGrid")

                    .Datasource(Model)

                    .Columns(col =>

                    {

                        col.Field("EventId").HeaderText("Event ID").TextAlign(TextAlign.Center).Width(75).Add();

                        col.Field("EventTypeDescription").HeaderText("Event Description").TextAlign(TextAlign.Center).Width(75).Add();

                        col.Field("CreatedOn").HeaderText("Created On").TextAlign(TextAlign.Center).Width(75).Add();

                        col.Field("Status").HeaderText("Status").TextAlign(TextAlign.Center).Width(75).Add();

                        col.Field("Message").HeaderText("Message").TextAlign(TextAlign.Center).Width(75).Add();
                    })
)

Model in this case, is @model List<API_EventListDTO>.

However now I need to bind a new grid to a specific property of the model which is a list of items. How can I do that? What I need to write in @(Html.EJ().Grid<API_EventListDTO>("EventListGrid") and in .Datasource(Model)??

Thank you

1 Reply

SE Sathyanarayanamoorthy Eswararao Syncfusion Team April 11, 2018 05:02 PM UTC

Hi Samuel, 

Thanks for contacting Syncfusion support. 

According to your query we suspect that you need to bound the list as dataSource for the grid using Model. We have achieved this requirement in the following example. 


Refer the below code example. 


 
 
@model EJGrid.Controllers.HomeController.newclass 
 
@(Html.EJ().Grid<Orders>("FlatGrid") 
              .Datasource(Model.empID) 
              .AllowPaging() 
                   
                    ….. 
 
) 
 
[Controller.cs] 
public class HomeController : Controller 
    { 
        public static List<Orders> order = new List<Orders>(); 
        public static List<EmpID> emp = new List<EmpID>(); 
 
        public ActionResult Index() 
        { 
 
            List<EmpID> emp1 = new List<EmpID>() { new EmpID() { ID = 1, Name = "Ewddada" }}; 
            List<EmpID> emp2 = new List<EmpID>() { new EmpID() { ID = 2, Name = "Caxada" }}; 
            List<EmpID> emp3 = new List<EmpID>() { new EmpID() { ID = 3, Name = "Nmbddj" }}; 
            List<EmpID> emp4 = new List<EmpID>() { new EmpID() { ID = 4, Name = "Mbgfuydf" }}; 
            List<EmpID> emp5 = new List<EmpID>() { new EmpID() { ID = 5, Name = "Nuhjbsd" }}; 
            order.Add(new Orders( 1, "VINET", emp1, 32.38, new DateTime(2014, 12, 25), "Reims")); 
            order.Add(new Orders( 2, "TOMSP", emp2, 11.61, new DateTime(2014, 12, 21), "Munster")); 
            order.Add(new Orders( 3, "ANATER",emp3, 45.34, new DateTime(2014, 10, 18), "Berlin")); 
            order.Add(new Orders( 4, "ALFKI", emp4, 37.28, new DateTime(2014, 11, 23), "Mexico")); 
            order.Add(new Orders( 5, "FRGYE", emp5, 67.00, new DateTime(2014, 05, 05), "Colchester")); 
 
            newclass newModel = new newclass(); 
            newModel.empID = emp1; 
            newModel.ord = order; 
            return View(newModel); 
 
        } 
        public class newclass { 
            public List<EmpID> empID { get; set; } 
            public List<Orders> ord { get; set; } 
        }     
    } 
 
[Model class] 
 
  public class Orders 
    { 
        public Orders() 
        { 
 
        } 
        public Orders(int orderId, string customerId, List<EmpID> empId, double freight, DateTime orderDate, string shipCity) 
        { 
            this.OrderID = orderId; 
            this.CustomerID = customerId; 
            this.EmployeeID = empId; 
            this.Freight = freight; 
            this.OrderDate = orderDate; 
            this.ShipCity = shipCity; 
        } 
        public int? OrderID { get; set; } 
        public string CustomerID { get; set; } 
        public List<EmpID> EmployeeID { get; set; } 
        public double? Freight { get; set; } 
        public DateTime? OrderDate { get; set; } 
        public string ShipCity { get; set; } 
    } 
    public class EmpID 
    { 
        public int ID { get; set; } 
        public string Name { get; set; } 
    } 
 

In the above code example Orders is the model which contains a property EMPID which is a list of items. In the controller page we have stored the list objects of the Orders and EMPID in a new class objects and passed it as a model. In this way the required list can be accessed using Model parameter in view page. 

Regards, 
Sathyanarayanamoorthy 


Loader.
Up arrow icon