我想为需要大量过滤和分组的应用程序使用Telerik MVC Grid ...在每个模型中,它都有一个用于存储CreationDate的属性DateTime。有时向用户显示网格时,时间并不重要。此外,我必须使用ViewModels来避免循环引用,因为我使用的是LINQ。Telerik MVC Grid - 按日期分组
问题出现时按日期对结果进行分组或分组。如果我在我的ViewModel上使用CreationDate字段作为日期时间,然后在视图上给它格式以仅显示日期,它排序正常,工作正常,但在使用整个日期时间值对其进行分组时,所以不会被分组。如果我在ViewModel中使用CreationDate作为字符串,它会在第一次显示罚款,但如果按日期分组或按日期分组,则会发生错误。
下面是我对这种情况下的代码:
视图模型:
public class CenterViewModel
{
public int Id { get; set; }
public string Name{ get; set; }
public string CityName { get; set; }
public string Phone{ get; set; }
public string CreationDate { get; set; }
public bool Active { get; set; }
}
控制器:
[GridAction]
public ActionResult AjaxIndex()
{
var model = repository.GetAllRecords()
.Select(o => new CenterViewModel
{
Id = o.Id,
Name = o.Name,
CityName= o.City.Name,
Phone = o.Phone,
CreationDate = o.CreationDate .ToShortDateString(),
Active = o.Active
});
return View(new GridModel
{
Data = model
});
}
public ActionResult Index()
{
return View();
}
VIEW:
@model IEnumerable<CenterViewModel>
@(Html.Telerik().Grid<CenterViewModel>()
.Name("Grid")
.DataKeys(keys =>
{
keys.Add(p => p.Id);
})
.Columns(columns =>
{
columns.Bound(o => o.Name);
columns.Bound(o => o.CityName);
columns.Bound(o => o.Phone);
columns.Bound(o => o.CreationDate).Width(200);
columns.Bound(o => o.Active).Width(100)
.DataBinding(dataBinding => {
dataBinding.Ajax().Select("AjaxIndex", "Centers", null);
})
.Pageable()
.Sortable()
.Groupable()
.Filterable())
上面的代码只会工作为杉木数据加载时,当您按日期分组时,它会抛出以下异常:“方法'System.String ToShortDateString()'没有支持的SQL转换。”这是有道理的,但是,我认为我的意图现在很清楚。
有谁知道如何解决这个问题?在此先感谢,
这很奇怪。在我目前的项目中,我使用相同的代码。绑定与'columns.Bound(o => o.Created).Width(100);'和模型'Created = dto.Created.ToShortDateString(),'。我可以排序,分组和其他一切。你使用ORM并返回GetAllRecords中的physycal实体吗?如果是这样,那将是我们唯一的区别。我将返回一个已从实体映射的DTO的IEnumerable,然后再将它们分配给视图模型。也许它与此有关。 – Nope 2012-02-14 17:33:17