Search code examples
asp.net-mvcasp.net-mvc-4kendo-uikendo-gridkendo-asp.net-mvc

How bind data source for kendo grid using stored procedure


First of all this is my first work using kendo ui. In work i have some data from database, i would like to replace my mvc webgrid into impressive kendo grid. I have created a list from database and iam trying to bind into kento grid. After setting data source by calling stored procedure. Still the grid remains empty. controller:

public ActionResult index()
        {
            return View();
        }  
public JsonResult Getuser([DataSourceRequest] DataSourceRequest request)
        {
            TestDB_Emp db = new TestDB_Emp();

            var employees = db.sps_selectemp();
            DataSourceResult response = employees.ToDataSourceResult(request);
            return Json(response, JsonRequestBehavior.AllowGet);

        }

view

@(Html.Kendo().Grid<webkendo.sps_selectemp_Result>()
        .Name("grid")
        .Columns(columns =>
        {
            columns.Bound(c => c.FirstName);

            columns.Bound(c => c.SecondName);
            columns.Bound(c => c.Email);
            columns.Bound(c => c.Gender).Width(150);
        })
        .HtmlAttributes(new { style = "height: 550px;" })
        .Scrollable()
        .Groupable()
        .Sortable()
        .Pageable()
            .DataSource(dataSource => dataSource
            .Ajax()
            .Read(read => read.Action("Getuser", "Home"))
            .PageSize(20)
        )
)

class

namespace webkendo
{
    using System;

    public partial class sps_selectemp_Result
    {
        public string City { get; set; }
        public string FirstName { get; set; }
        public string SecondName { get; set; }
        public string Gender { get; set; }
        public string Email { get; set; }
        public string Mobile { get; set; }
        public int EmpID { get; set; }
        public Nullable<System.DateTime> DOB { get; set; }
    }
}

What is missing on my code.


Solution

  • Try without using entity model when using stored procedure.

    public ActionResult Results([DataSourceRequest] DataSourceRequest request)
            {
                var page = request.Page;
                var pagesize = request.PageSize;
                SqlConnection sqcon = new SqlConnection(conn);
                SqlCommand cmd = new SqlCommand();
                SqlDataAdapter sd = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                cmd.Connection = sqcon;
                cmd.CommandText = "sps_selectemp";
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                sqcon.Open();
                sd.Fill(dt);
                sqcon.Close();
                List<EmployeeDetails> StudentList = new List<EmployeeDetails>();
                foreach (DataRow dr in dt.Rows)
                {
                    EmployeeDetails st = new EmployeeDetails();
                    st.ID = Convert.ToInt32(dr["EmpID"]);
                    st.FirstName = dr["FirstName"].ToString();
                    st.SecondName = dr["SecondName"].ToString();
                    st.Email = dr["Email"].ToString();
                    st.Gender = dr["Gender"].ToString();
                    st.Mobile = dr["Mobile"].ToString();
    
                    st.City = dr["City"].ToString();
    
    
    
                    StudentList.Add(st);
                }
                DataSourceResult response = StudentList.ToDataSourceResult(request);
                return Json(response, JsonRequestBehavior.AllowGet);
    
            }