2012-02-14 44 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