【问题标题】:Binding data source in kendo grid on mvc在 mvc 上的剑道网格中绑定数据源
【发布时间】:2019-03-29 15:32:41
【问题描述】:

首先,这是我使用剑道 ui 的第一部作品。在工作中,我有一些来自数据库的数据,我想将我的 mvc webgrid 替换为令人印象深刻的剑道网格。我已经从数据库创建了一个列表,并试图绑定到 kento 网格。设置数据源后。网格仍然是空的。

 public ActionResult Index()
        {
            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.State = dr["State"].ToString();
                st.City = dr["City"].ToString();
                st.Country = dr["Country"].ToString();


                StudentList.Add(st);
            }
            return View(StudentList.ToList());
        }

然后我为相应的视图添加了一个视图

    @model List<webkendo.Models.EmployeeDetails>
    @(Html.Kendo().Grid<webkendo.Models.EmployeeDetails>()
            .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()

            .DataSource(dataSource => dataSource
                .Ajax()
                .Read(read => read.Action("getusers", "Home"))
                .PageSize(20)
            )
    )

还是尝试了不同的方法

 public List<EmployeeDetails> getusers()
        {
            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.State = dr["State"].ToString();
                st.City = dr["City"].ToString();
                st.Country = dr["Country"].ToString();


                StudentList.Add(st);
            }
            return StudentList;
        }

我做错了什么

【问题讨论】:

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


    【解决方案1】:

    首先,决定您是要获取服务器端的所有数据并将其显示在网格中,还是要使用带有分页的 AJAX 等,这对于较长的列表更好。您正在尝试两者兼而有之。

    首先,你需要去掉Read并设置ServerOperation(false):

    // Your model is the list of data
    @(Html.Kendo().Grid(Model)
    ...
    // Tell kendo you are providing the data
    .DataSource(dataSource => dataSource
        .Ajax()
        .ServerOperation(false)
        .PageSize(20)
        // No Read since you provide all the data up front
     )
    

    对于第二个选项:

    // Tell kendo the type you are going to fetch in the Read
    @(Html.Kendo().Grid<EmployeeDetails>()
    ...
    // Tell kendo you want data retrieved via AJAX
    .DataSource(dataSource => dataSource
        .Ajax()
        .Read(read => read.Action("getusers", "Home"))
        .PageSize(20)
     )
    

    现在创建您的读取操作以返回 JSON 并利用处理分页、过滤、排序等的 Kendo 的 DataSourceRequest。

    public JsonResult getusers([DataSourceRequest] DataSourceRequest request)
    {
        // The AJAX generally works with IQueryables so that it can select a 
        // page full or records at a time. Entity Framework makes this easy.
        // You would need to amend for ADO.NET with stored proc.
        var employees = _db.Employees;
        DataSourceResult response = employees.ToDataSourceResult(request);
        return Json(response, JsonRequestBehavior.AllowGet);
    }
    

    【讨论】:

    • 我又问了一个与剑道有关的问题@steve Greene。
    • @Steve Greene - 谢谢你的第二个选项。我正在尝试您在我的 asp.net Core 应用程序中提到的第二种绑定方式。 Action 方法被调用并获取 JSON,但 Grid 没有显示任何内容。我看到了许多与您类似的答案,但由于所有答案中都缺少这条线,所以没有奏效 - DataSourceResult response = employees.ToDataSourceResult(request);
    • @Jith 巧合的是,我自己又遇到了同样的问题。坐下来盯着一个空的网格,没有脚本错误。然后意识到我需要 ToDataSourceResult。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-26
    • 1970-01-01
    • 2014-04-04
    相关资源
    最近更新 更多