English 中文(简体)
Telerik MVC Grid 日期:
原标题:Telerik MVC Grid - Group by Date

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,

最佳回答

One thing you could try is in your model to use a date that removes the time element. Then in the view, format the date.

观点:

public DateTime CreationDate { get; set; }

主计长:

var model = repository.GetAllRecords()
    .Select(o => new CenterViewModel
    {
        Id = o.Id,
        Name = o.Name,
        CityName = o.City.Name,
        Phone = o.Phone,
        CreationDate = new DateTime(o.CreationDate.Year, o.CreationDate.Month, o.CreationDate.Day),
        Active = o.Active
    });

观点:

columns.Bound(o => o.CreationDate).Width(200).Format("{0:MM/dd/yyyy}");
问题回答

暂无回答




相关问题
Discuss on Commercial Component libraries of silverlight

I am now looking forward to buy a component library of silverlight for increase the productivity. I find there are number of them. Telerik ComponentOne ComponentArt Infragistics Syncfusion I found ...

Telerik RadGrid: grid clientside pagination

I have a web service which returns me some data,I am massaging this data and using this as datasource for my radgrid (telerik). The datasource is quite large, and would like to paginate it. I found ...

Binding a radgrid to a tree like data structure

I have a structure that looks following Class TreeNode { public TreeNode Parent { get; } public IEnumerable<TreeNode> Children { get; } public . . . . } I want to bind this to a ...

IQueryable into a hierarchy

I currently have an IQueryable of Questions. In my Question object I have and "id" and a "parentId" which can be used to create a hierarchy. Currently, I bind a RadTreeView to the IQueryable of ...

Expand all items in RadGrid Hierarchy

I am using a RadGrid (2009 Q2) with a hierarchy. Is there a way in the client api to expand all rows and vice-versa? thanks! Update: I have written a javascript function based off the api ...

热门标签