通过连接到asp.net MVC方法过滤剑道网格中的数据
本文关键字:网格 数据 过滤 方法 连接 asp MVC net | 更新日期: 2023-09-27 18:12:44
我正在使用ASP编写代码。。NET MVC与剑道网格。我想把数据从数据库到剑道网格等于具体日期或默认"今天"。
我想在工具栏上做一个日期选择器和一个按钮,每次我点击按钮,它都会向控件发送一个请求,并在LINQ中过滤数据,并在一天内发送所有数据的请求。我写了下面的代码:
控制器类:
//main method
public ActionResult LogAdminList()
{
return View();
}
//submethod for grid
public ActionResult Grid_ReadLogAdminList([DataSourceRequest] DataSourceRequest request, [Bind(Prefix = "id")] string date)
{
DateTime _temp;
if (!DateTime.TryParse(date, out _temp))
_temp = DateTime.Now;
return Json(_context.Entities<LogAdmin>().NoTracking().OrderByDescending(l => l.TimeStamp)
.Where(f => DbFunctions.TruncateTime(f.TimeStamp) == DbFunctions.TruncateTime(_temp))
.Select(l => new LogAdminInfo
{
Id = l.Id,
Message = l.Message,
MessageTemplate = l.MessageTemplate,
Level = l.Level,
TimeStamp = l.TimeStamp,
Exception = l.Exception,
Properties = l.Properties,
LogEvent = l.LogEvent,
})
.ToDataSourceResult(request));
}
剑道代码为:
@(Html.Kendo().Grid<LogAdminInfo>()
.BindTo(Model)
.Name("LogAdminList")
.Sortable()
.Columns(columns =>
{
columns.Bound(p => p.Message).Width(50).Title(WebResources.LogListMessage);
columns.Bound(p => p.MessageTemplate).Width(50).Title(WebResources.LogListMessageTemplate);
columns.Bound(p => p.Level).Title(WebResources.LogListLevel);
columns.Bound(p => p.TimeStamp).Title(WebResources.LogListTimeStamp).Format("{0:dd.MM.yyyy H:mm}");
columns.Bound(p => p.Exception).Width(50).Title(WebResources.LogListException);
columns.Bound(p => p.Properties).Width(50).Title(WebResources.LogListProperties);
columns.Bound(p => p.LogEvent).Title(WebResources.LogListLogEvent);
})
.Pageable(pageable => pageable
.Refresh(true)
.ButtonCount(5))
.HtmlAttributes(new { @class = "grd_UpcomingMilestone" })
.ToolBar(toolbar =>
{
toolbar.Template(@<text>
<div class="toolbar">
<label class="category-label" for="category">@WebResources.LogFilterMessage</label>
@(Html.Kendo().DatePicker()
.Name("datepicker")
.Value(DateTime.Today)
.Format("dd.MM.yyyy")
)
@(Html.Kendo().Button()
.Name("filterButton")
.Icon("filter")
.Content("Filter")
.HtmlAttributes(new { type = "button" })
)
</div>
</text>);
})
.DataSource(source => source
.Ajax()
.Batch(true)
.ServerOperation(false)
.PageSize(100)
.Read(read => read.Action("Grid_ReadLogAdminList", "LogAdmin").Data("getFilterDate"))
))
我不知道在javascript中该怎么做。代码在第一个请求中工作得很好,但当我过滤时就不行了。
我应该写什么作为一个javascript?
非常感谢。
您必须使用Kendo Grid Read Data
方法中指定的getFilterDate
函数将数据从客户端传递到服务器。实现这样一个函数。返回类型必须是一个与您在MVC动作中定义的相同名称的字段的对象,在本例中为date
。Kendo将序列化它们以构建get请求:
function getFilterDate() {
var mydate = $("#datepicker").data("kendoDatePicker").value();
return {
"date": mydate
}
}