2012-02-14 61 views
2

我想为需要大量过滤和分组的应用程序使用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转换。”这是有道理的,但是,我认为我的意图现在很清楚。

有谁知道如何解决这个问题?在此先感谢,

+0

这很奇怪。在我目前的项目中,我使用相同的代码。绑定与'columns.Bound(o => o.Created).Width(100);'和模型'Created = dto.Created.ToShortDateString(),'。我可以排序,分组和其他一切。你使用ORM并返回GetAllRecords中的physycal实体吗?如果是这样,那将是我们唯一的区别。我将返回一个已从实体映射的DTO的IEnumerable,然后再将它们分配给视图模型。也许它与此有关。 – Nope 2012-02-14 17:33:17

回答

2

您可以尝试的一件事是在您的模型中使用删除时间元素的日期。然后在视图中格式化日期。

视图模型:

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 
       }); 

VIEW:

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

'CreationDate'是'CenterViewModel'中的'string'而不是'DateTime'对象。最后你错过了'.ToString()'。对于'string',我也无法获得'.Format(“{...}”)'来改变显示值的格式。但是,这可能只是我,可能对'met.lord'很好。 – Nope 2012-02-14 20:43:37

+0

@FrançoisWahlCreationDate在模型中最后有一个.ToShortDateString(),这表明它是DateTime对象而不是字符串。我建议将它保留为DateTime而不是将其转换为字符串。在该视图中,我建议的.Format格式仅适用于DateTime。 – Daniel 2012-02-14 21:09:02

+0

@FrançoisWahl我改变了我的答案,以便ViewModel将CreationDate作为DateTime。 – Daniel 2012-02-14 21:14:13