Asp.net mvc 3 Telerik MVC电网-按日期分组

Asp.net mvc 3 Telerik MVC电网-按日期分组,asp.net-mvc-3,telerik,telerik-grid,telerik-mvc,Asp.net Mvc 3,Telerik,Telerik Grid,Telerik Mvc,我正在尝试将Telerik MVC网格用于需要大量过滤和分组的应用程序。。。在我拥有的每个模型上,它都有一个用于存储CreationDate的属性DateTime。有时向用户显示网格时,时间并不重要。此外,由于使用LINQ,我不得不使用ViewModels来避免循环引用 在按日期对结果进行重新排序或分组时会出现问题。如果我在ViewModel上使用CreationDate字段作为Datetime,然后在视图中为其指定格式以仅显示日期,则它排序良好,工作正常,但在使用整个Datetime值对其分组

我正在尝试将Telerik MVC网格用于需要大量过滤和分组的应用程序。。。在我拥有的每个模型上,它都有一个用于存储CreationDate的属性DateTime。有时向用户显示网格时,时间并不重要。此外,由于使用LINQ,我不得不使用ViewModels来避免循环引用

在按日期对结果进行重新排序或分组时会出现问题。如果我在ViewModel上使用CreationDate字段作为Datetime,然后在视图中为其指定格式以仅显示日期,则它排序良好,工作正常,但在使用整个Datetime值对其分组时,将永远不会有任何groupedby。如果我在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; }
    }
public DateTime CreationDate { 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();
    }
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
                });
视图:

@model IEnumerable
@(Html.Telerik().Grid())
.名称(“网格”)
.DataKeys(keys=>
{
key.Add(p=>p.Id);
})
.列(列=>
{
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(数据绑定=>{
Ajax().Select(“AjaxIndex”,“Centers”,null);
})
.Pageable()
.Sortable()
.Groupable()
.Filterable())
上面的代码只适用于第一次加载的数据,当您使用或按日期分组时,它将抛出以下异常:“方法'System.String ToSortDateString()'不支持转换为SQL。”这是有道理的,但是,我想我的意图现在已经很清楚了


有人知道如何解决这个问题吗?请提前感谢,

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

视图模型:

    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; }
    }
public DateTime CreationDate { 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();
    }
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.Created).Width(100);
和model
Created=dto.Created.ToShortDateString(),
。我可以排序、分组和其他一切。您是否使用ORM并在GetAllRecords中返回物理实体?如果是这样,这将是我们唯一的区别。我将返回一个IEnumerable的DTO,这些DTO在分配给视图模型之前已从实体映射。可能与此相关。
CreationDate
CenterViewModel
中的
字符串
而不是
日期时间
对象。您最后缺少了
.ToString()
。使用
字符串
我也无法获得
格式(“{…}”)
要对显示的值的格式进行任何更改。但这可能只是我一个人的问题,对于
met可能非常有效。lord
@FrançoisWahl CreationDate在模型中有一个.ToShortDateString()最后,这向我表明它是一个DateTime对象,而不是字符串。我建议op将其保留为DateTime,而不是将其转换为字符串。在视图中,我建议的.Format仅适用于DateTime。@FrançoisWahl我更改了答案,以便ViewModel将CreationDate作为DateTime。
@Daniel
可能有点问题我想把它变成一个
DateTime
。这可能也会使整个.Format工作起来。我也会在我自己的工作项目上尝试。谢谢你的提示。这实际上是一个好主意,因为当你设置一个新的DateTime对象时,它会为每个元素设置相同的时间(上午12点)值。然后,它会被正确分组。谢谢!