Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind the Kendo Grid in asp.net MVC 4 Razor

I have create the asp.net MVC 4 application where i am using the entity framework and class "Data" is the model.

AdventureWorksTrainingEntities _dbContext = new AdventureWorksTrainingEntities();
Data _data = new Data();  //Model

Now i want to display the data of the table to the kendo grid. In the controller, i am using the following code:

 public ActionResult Index()
        {
           List<Movie> dataForGrid= _dbContext.Movies.ToList();
           return View(dataForGrid);
        }

Now i have no idea for displaying the data in Kendo Grid (i am new to kendo and MVC). I have also tried the following but not working:

@model   IEnumerable<MvcApp.Models.Data>
@using Kendo.Mvc.UI 
@{
    ViewBag.Title = "Index";
}

<h2>Grid For Data</h2>
Html.Kendo().Grid<Order>()
    .Name("Grid")
    .DataSource(dataSource => dataSource // Not implemented
)
like image 354
Bhupendra Shukla Avatar asked Aug 27 '13 12:08

Bhupendra Shukla


1 Answers

Finally got answer:

View:

@(Html.Kendo().Grid<KendoUI.Models.EmployeeViewModel>()
            .Name("Grid")
            .Columns(columns =>
            {
                columns.Bound(p => p.name).Title("Name");
                columns.Bound(p => p.gender).Title("Gender");
                columns.Bound(p => p.designation).Title("Designation").Width("300px");
                columns.Bound(p => p.department).Title("Department").Width("300px");
            })

            .Editable(editable => editable.Mode(GridEditMode.InLine))
            .Navigatable() 
            .Pageable()
            .Sortable()
            .Scrollable()
            .DataSource(dataSource => dataSource // Configure the grid data source
            .Ajax()
            .Model(model =>
            {
                model.Id(x => x.id);
            })
                .Read(read => read.Action("Employee_Read", "Home")) // Set the action method which will return the data in JSON format  
             )
            )

Controller:

 public ActionResult Employee_Read([DataSourceRequest]DataSourceRequest request)
        {
            IQueryable<Bhupendra_employees> Details = _dbContext.Bhupendra_employees;
            DataSourceResult result = Details.ToDataSourceResult(request, p => new EmployeeViewModel
                    {
                        id = p.id,
                        name = p.name,
                        gender = p.gender,
                        designation = p.designation,
                        department = p.Bhupendra_Dept.Dept_Description
                    });
            return Json(result, JsonRequestBehavior.AllowGet);
        }

Model:

public class EmployeeViewModel
    {
        public Int32 id { get; set; }
        public String name { get; set; }
        public String gender { get; set; }
        public String designation { get; set; }
        public String department { get; set; }
        //public DateTime dob { get; set; }
    }
like image 199
Bhupendra Shukla Avatar answered Sep 21 '22 07:09

Bhupendra Shukla