7

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
)

2 Answers 2

11

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; }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

what about if this same is in jquery initialization?
4

if your controller name is Data then you can use the following

Your Model

 public ActionResult ReadDegrees([DataSourceRequest] DataSourceRequest request)
    {
        ViewBag.Countries = CommonController.CountryList();
        ViewBag.DegreeTypes = CommonController.DegreeTypeList();
        using (var _dbContext= new AdventureWorksTrainingEntities ())
        {
            return Json(_dbContext.Movies.ToList.ToDataSourceResult(request));
        }
    }

Your View Just you need to add the following assuming that your Model has a Key called ID

Html.Kendo().Grid<Order>()
.Name("Grid")
.DataSource(dataSource => dataSource
    .Ajax()
    .PageSize(20)
    .Model(model => model.Id(p => p.ID))
    .Read(read => read.Action("ReadDegrees", "Data"))))

2 Comments

.Read(read => read.Action("Index", "Data")))) i guess instead of index it should be ReadDegrees?
i am getting error on Html.KendoGrid Htmlhelper does not contain definition of 'Kendo'..but i have added reference of Kendo.MVC.dll in web.config of both View as well as Project also. Did you know how to remove error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.