我想爲需要大量過濾和分組的應用程序使用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