如何使用存储过程为 kendo 网格绑定数据源
How bind data source for kendo grid using stored procedure
首先,这是我使用 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)
)
)
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; }
}
}
我的代码缺少什么。
尝试在使用存储过程时不使用实体模型。
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);
}
首先,这是我使用 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)
)
)
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; }
}
}
我的代码缺少什么。
尝试在使用存储过程时不使用实体模型。
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);
}