Asp.net mvc 5 基于mvc的剑道网格数据源绑定

Asp.net mvc 5 基于mvc的剑道网格数据源绑定,asp.net-mvc-5,kendo-grid,kendo-asp.net-mvc,kendo-ui-mvc,Asp.net Mvc 5,Kendo Grid,Kendo Asp.net Mvc,Kendo Ui Mvc,首先,这是我第一次使用剑道ui。在工作中,我从数据库中获得了一些数据,我想将我的mvc webgrid替换成令人印象深刻的剑道网格。我已经从数据库创建了一个列表,我正试图绑定到肯托网格。设置数据源后。网格仍然是空的 public ActionResult Index() { SqlConnection sqcon = new SqlConnection(conn); SqlCommand cmd = new SqlCommand(

首先,这是我第一次使用剑道ui。在工作中,我从数据库中获得了一些数据,我想将我的mvc webgrid替换成令人印象深刻的剑道网格。我已经从数据库创建了一个列表,我正试图绑定到肯托网格。设置数据源后。网格仍然是空的

 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());
        }
public ActionResult Index()
{
SqlConnection sqcon=新的SqlConnection(conn);
SqlCommand cmd=新的SqlCommand();
SqlDataAdapter sd=新的SqlDataAdapter(cmd);
DataTable dt=新的DataTable();
cmd.Connection=sqcon;
cmd.CommandText=“sps_selectemp”;
cmd.CommandType=System.Data.CommandType.StoredProcess;
sqcon.Open();
标准差填充(dt);
sqcon.Close();
List StudentList=新列表();
foreach(数据行dr在dt.行中)
{
EmployeeDetails st=新的EmployeeDetails();
st.ID=转换为32(dr[“EmpID”]);
st.FirstName=dr[“FirstName”].ToString();
st.SecondName=dr[“SecondName”].ToString();
st.Email=dr[“Email”].ToString();
st.Gender=dr[“性别”]。ToString();
st.Mobile=dr[“Mobile”].ToString();
st.State=dr[“State”].ToString();
圣城=博士[“城市”]。ToString();
st.Country=dr[“Country”]。ToString();
学生名单。添加(st);
}
返回视图(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)
            )
    )
@型号列表
@(Html.Kendo().Grid())
.名称(“网格”)
.列(列=>
{
columns.Bound(c=>c.FirstName);
columns.Bound(c=>c.SecondName);
columns.Bound(c=>c.Email);
columns.Bound(c=>c.Gender).Width(150);
})
.HtmlAttributes(新的{style=“height:550px;”})
.Scrollable()
.Groupable()
.Sortable()
.DataSource(DataSource=>DataSource
.Ajax()
.Read(Read=>Read.Action(“getusers”、“Home”))
.页面大小(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;
        }
公共列表getusers()
{
SqlConnection sqcon=新的SqlConnection(conn);
SqlCommand cmd=新的SqlCommand();
SqlDataAdapter sd=新的SqlDataAdapter(cmd);
DataTable dt=新的DataTable();
cmd.Connection=sqcon;
cmd.CommandText=“sps_selectemp”;
cmd.CommandType=System.Data.CommandType.StoredProcess;
sqcon.Open();
标准差填充(dt);
sqcon.Close();
List StudentList=新列表();
foreach(数据行dr在dt.行中)
{
EmployeeDetails st=新的EmployeeDetails();
st.ID=转换为32(dr[“EmpID”]);
st.FirstName=dr[“FirstName”].ToString();
st.SecondName=dr[“SecondName”].ToString();
st.Email=dr[“Email”].ToString();
st.Gender=dr[“性别”]。ToString();
st.Mobile=dr[“Mobile”].ToString();
st.State=dr[“State”].ToString();
圣城=博士[“城市”]。ToString();
st.Country=dr[“Country”]。ToString();
学生名单。添加(st);
}
返回学生名单;
}

我做错了什么首先,决定是获取所有数据服务器端并将其显示在网格中,还是将AJAX与分页等结合使用。对于较长的列表,这一点更好。你正试图两者兼而有之

对于第一种情况,您需要取消读取和设置服务器操作(false):

对于第二种选择:

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

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

对于第一种情况,您需要取消读取和设置服务器操作(false):

对于第二种选择:

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

我又问了一个关于剑道的问题。@steve Greene@steve Greene-谢谢你的第二个选择。我正在尝试您在我的asp.net核心应用程序中提到的第二种绑定方式。正在调用Action方法并获取JSON,但网格没有显示任何内容。我看到了许多与您类似的答案,但都不起作用,因为所有答案中都缺少这一行-
DataSourceResult response=employees.ToDataSourceResult(request)@Jith巧合的是,我自己又遇到了同样的问题。坐下来盯着一个空的网格,没有脚本错误。然后意识到我需要ToDataSourceResult。我又问了一个与kendo@steve Greene有关的问题。@steve Greene-谢谢你的第二个选择。我正在尝试您在我的asp.net核心应用程序中提到的第二种绑定方式。正在调用Action方法并获取JSON,但网格没有显示任何内容。我看到了许多与您类似的答案,但都不起作用,因为所有答案中都缺少这一行-
DataSourceResult response=employees.ToDataSourceResult(request)@Jith巧合的是,我自己又遇到了同样的问题。坐下来盯着一个空的网格,没有脚本错误。然后意识到我需要ToDataSourceResult。
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);
}