在ActionResult和View之间转换数据 [英] convert Data between ActionResult and View

查看:233
本文介绍了在ActionResult和View之间转换数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我使用kendo网格显示数据,而在ActionResult中,我使用以下代码获取数据:

In my project i use kendo grid to show data and in my ActionResult i use this code to get data :

public ActionResult ShowData([DataSourceRequest] DataSourceRequest request, long studentid)
{
    IQueryable<Models.Registration.RegistrationModel>
        query =
        from financeItem in db.RegistrationItems

        select new Models.Registration.RegistrationModel
        {
            RegisterId = financeItem.RegistrationId,
            Id = financeItem.Registration.Id,
            Date = financeItem.Registration.Date,

            StudentName = financeItem.Registration.Student.FirstName + " " + financeItem.Registration.Student.LastName
        };
    DataSourceResult dsr = query.ToDataSourceResult(request);

    return Json(dsr, JsonRequestBehavior.AllowGet);
}

如此.我需要将日期转换为本地日期,但是我遇到了问题. 这是我的问题: 当我想在查询中转换日期时,我收到许多linq2sql找不到转换器函数的错误.所以我将查询类型从IQueryable更改为list并尝试convert.但由于有很多记录,所以出现超时错误. 发生此错误后,我尝试像这样的代码在viewmodel中的get和set属性中转换数据:

so . I need convert date to local date and I have a problem. this is my problem : when I want convert date inside query I get many error that linq2sql can't find converter function. so I change query type from IQueryable to list and try convert . but I get time out error because I've got a lot of records. after this error I try convert data inside get and set property in viewmodel like this code :

public string DateSs
{
    get
    {

        return DateConvertor(Date.ToString());
    }
    set { value.ToString(); }
}

,然后在视图中使用此属性并显示转换后的数据. 到目前为止,一切工作正常,但是当我想在剑道网格中使用过滤器部件并根据输入日期过滤数据库时,我得到了该文件未在查询中退出的错误. 所以我头疼,我不知道该怎么办,以及如何转换此日期

and I use this property inside view and show converted data. Everything works fine so far but when I want use filter part in kendo grid and filter data base on input date I get and error that this filed not exited in query. so I have a Headache and I don't know what must I do and how can I convert this dame date

推荐答案

对于Kendo Grid,日期"列表示的是 DateSs 而不是 Date .尝试对Date应用过滤器时,它对数据库表中不存在的DateSs应用过滤器,如果在查询中定义,则会引发错误.

For Kendo Grid your Date column is representation for DateSs instead of Date. When it tries to apply filter on Date, it is applying filter on DateSs which doesn't exist in database table and throws error if defined in your query.

我认为解决方案是在应用 ToDataSourceResult 之前拦截 DataSourceRequest ,并相应地更改值和过滤器名称

I think the solution for that would be to intercept the DataSourceRequest before applying ToDataSourceResult and change the value and filter name accordingly

更新

Kendo Grid发送的过滤器位于request.Filters中,但结构是分层的,可以在一个请求中支持多个过滤器.因此,首先,您可能想使过滤器变平整,您可以使用此代码,

The filter Kendo Grid sent is in request.Filters but the structure is hierarchical to support multiple Filters in one request. So, first of all you might want to flatten the filter out, you can use this code to that,

public static IEnumerable<IFilterDescriptor> FlattenFilters(this DataSourceRequest request)
{
        return request.Filters
            .RecursiveSelect(
                descriptor => (descriptor as CompositeFilterDescriptor)?.FilterDescriptors ?? Enumerable.Empty<IFilterDescriptor>(),
                descriptor => descriptor as FilterDescriptor)
            .Where(x => x != null);
}

此函数将返回当前应用的所有过滤器的Enumerable.然后您可以像这样更改过滤器的名称

This function will return an Enumerable of all the filters currently applied. Then you can change the name of filter like this

request.FlattenFilters().Each(x =>
{
            FilterDescriptor filter = x as FilterDescriptor;
            if (filter != null && filter.Member == "DateSs")
            {
                filter.Member = "Date";

                //Change the filter.Value property according to your case
                //i.e. It would be in String and you might want to convert it to date too.
            }
});

这篇关于在ActionResult和View之间转换数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆