[英]kendo ui grid for mvc read from viewbag
我有一个控制器,它通过Viewbag返回强类型视图和动态sql过程数据表的结果。 我正在尝试将网格绑定到视图中的Viewbag数据,但是我不知道该怎么做。
@(Html.Kendo().Grid(ViewBag.Rev)
.Name("RevGrid")
.Pageable()
.Sortable()
.Filterable()
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(20)
.ServerOperation(false)
))
public ActionResult Index()
{
using (DataContext db = new DataContext())
{
var spring = db.SpringTrainings.ToList();
ViewData["Rev"] = Revenue();
return View(spring);
}
}
这是数据表的来源:
public DataTable Revenue()
{
using (var conn = new System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
{
using (var commandproc = conn.CreateCommand())
{
commandproc.CommandType = System.Data.CommandType.StoredProcedure;
commandproc.CommandText = "stTicketRevenue";
conn.Open();
{
var dataTable = new DataTable();
var dataReader = commandproc.ExecuteReader();
dataTable.Load(dataReader);
ViewBag.Proc = dataTable;
conn.Close();
return dataTable;
}
}
}
}
我真的很感谢您的帮助。 我什至不确定这是否是从控制器传递数据表的正确方法。
@(Html.Kendo().Grid(ViewBag.Rev as System.Data.DataTable)
.Name("RevGrid")
.Pageable()
.Sortable()
.Filterable()
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(20)
.ServerOperation(false)
))
(要么)
@(Html.Kendo().Grid(ViewData["Rev"] as System.Data.DataTable)
.Name("RevGrid")
.Pageable()
.Sortable()
.Filterable()
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(20)
.ServerOperation(false)
))
如果您不将ViewBag或ViewData强制转换为DataTable或List,则会出现错误。 Err->无法将lambda表达式用作动态调度的操作的参数,除非先将其转换为委托或表达式树类型
而不是使用ViewBag或ViewData,而是使用Kendo read操作从db获取数据。 为了使页面加载非常快,并且在页面加载到用户后,kendo grid将进行ajax调用以将数据绑定到其中。
@(Html.Kendo().Grid(new List<UI.Models.TestViewModel>())
.Name("RevGrid")
.Pageable()
.Sortable()
.Filterable()
.DataSource(dataSource => dataSource
.Ajax()
.Read(read => read.Action("GetDataTableAction", "ControllerName"))
.PageSize(20)
.ServerOperation(false)
))
和控制器方法看起来像
public ActionResult GetDataTableAction([DataSourceRequest] DataSourceRequest request)
{
List<Model> model = new List<Model>();
DataSourceResult result = model.ToDataSourceResult(request);
return Json(result, JsonRequestBehavior.AllowGet);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.