I m trying to use Telerik MVC Grid for an application which requires lots of filtering and grouping... On every model I have, it has a DateTime
property for storing CreationDate
. Sometimes when showing the grid to the user, time isn t important. Also, I had to use ViewModels to avoid circular references since I m using LINQ.
The problem comes when resorting or grouping results by date. If I use the CreationDate
field as a DateTime
on my ViewModel and then give it a Format
on the View to show only the date, it sorts fine, works fine, but when grouping it groups using the whole datetime value, so there won t ever be anything grouped by. If I use the CreationDate
as a string in the ViewModel, it shows fine the first time but will give an error if re-sorting or grouping by date.
Here s the code I have for this case:
ViewModel:
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; }
}
Controller:
[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()
)
以上编码只可用于第一批数据,届时,你将重新组合或重新组合,从而放弃以下例外:“友好系统”。 努力争取到现在为止,没有任何经过支持的翻译——这是有意义的——但我认为,我的意图现在很明确。
Does anyone know how to solve this issue? Thanks in advance,