如何使用存储过程为Kendo网格绑定数据源

时间:2019-03-30 09:46:19

标签: asp.net-mvc asp.net-mvc-4 kendo-ui kendo-grid kendo-asp.net-mvc

首先,这是我使用kendo ui的第一个作品。在工作中,我有一些来自数据库的数据,我想将我的mvc webgrid替换为令人印象深刻的kendo网格。我已经从数据库中创建了一个列表,并且我试图绑定到Kento网格中。通过调用存储过程设置数据源之后。网格仍然是空的。 控制器:

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);

        }

视图

@(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)
        )
)

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; }
    }
}

我的代码中缺少什么。

1 个答案:

答案 0 :(得分:1)

使用存储过程时,请尝试不使用实体模型。

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);

        }