Telerik MVC Grid - 按日期分组

发布于 2025-01-05 21:56:40 字数 2145 浏览 1 评论 0原文

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

当按日期对结果进行排序或分组时就会出现问题。如果我在 ViewModel 上使用 CreationDate 字段作为 DateTime,然后在视图上给它一个 Format 以仅显示日期,它会排序很好,工作正常,但是在分组时使用整个日期时间值进行分组,因此永远不会有任何分组依据。如果我在 ViewModel 中使用 CreationDate 作为字符串,它第一次显示正常,但如果按日期重新排序或分组,则会出现错误。

这是我针对这种情况的代码:

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()
)

上面的代码仅适用于第一次加载数据,当您按日期重新排序或分组时,它将抛出以下异常:“Method 'System.String ToShortDateString()' 不支持对 SQL 的转换。”这是有道理的,但是,我认为我的意图现在已经很明确了。

有谁知道如何解决这个问题?提前致谢,

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()
)

The above code would work only for the first load of data, when you re-sort or group by date it will throw the following exception: "Method 'System.String ToShortDateString()' has no supported translation to SQL." which makes sense, but, I think my intention is pretty clear now.

Does anyone know how to solve this issue? Thanks in advance,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沒落の蓅哖 2025-01-12 21:56:40

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

视图模型:

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

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.

ViewModel:

public DateTime CreationDate { get; set; }

Controller:

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}");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文