Iman Iman - 1 year ago 94
C# Question

convert Data between ActionResult and View

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

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

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

Answer Source

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.

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


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
                descriptor => (descriptor as CompositeFilterDescriptor)?.FilterDescriptors ?? Enumerable.Empty<IFilterDescriptor>(),
                descriptor => descriptor as FilterDescriptor)
            .Where(x => x != null);

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.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download