This is a migrated thread and some comments may be shown as answers.

Filtering when using a ViewModel

4 Answers 483 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Keith
Top achievements
Rank 1
Keith asked on 27 Jan 2014, 03:26 PM
Hi all,

I use a ViewModel when binding to a grid.
However when using the filtering function I get the following:

Invalid property or field - 'UserName' for type: ErrorLog

Username is only a property in the ErrorLogViewModel

The code to fill my view model is:
public ActionResult ListErrors([DataSourceRequest]DataSourceRequest request)
        {
            IQueryable<ErrorLog> errorLogs = (IQueryable<ErrorLog>)db.ErrorLogs.Include(e => e.User).OrderByDescending(e => e.ErrorLogId);
            DataSourceResult result = errorLogs.ToDataSourceResult(request, errorLog => new ErrorLogViewModel
                {
                    ErrorLogId = errorLog.ErrorLogId,
                    Message = errorLog.AdditionalMessage,
                    Timestamp = errorLog.Timestamp,
                    UserName = errorLog.User.UserName
                });
            return Json(result);
        }

Thanks,
Keith. 









4 Answers, 1 is accepted

Sort by
0
Keith
Top achievements
Rank 1
answered on 30 Jan 2014, 09:03 PM
For anybody wondering, this sorted it:

IQueryable<ErrorLog> errorLogs = (IQueryable<ErrorLog>)db.ErrorLogs.Include(e => e.User).OrderByDescending(e => e.ErrorLogId);
            DataSourceResult result = errorLogs.Select(errorLog => new ErrorLogViewModel
                {
                    ErrorLogId = errorLog.ErrorLogId,
                    Message = errorLog.AdditionalMessage,
                    Timestamp = errorLog.Timestamp,
                    UserName = errorLog.User.UserName
                }).ToDataSourceResult(request);
            return Json(result);
0
K'Library
Top achievements
Rank 1
answered on 08 Sep 2016, 04:39 PM

Hi Keith,

I read

 http://www.telerik.com/forums/custom-predicate-building-when-filtering 

and write a small project to finish filtering when using a view model:

https://github.com/keith-leung/telerik-aspnet-mvc-grid-diffviewmodel

I hope it helps you and the others.

 

0
K'Library
Top achievements
Rank 1
answered on 08 Sep 2016, 04:45 PM

Another filtering sample thread:

http://www.telerik.com/forums/todatasourceresult

0
Maria Ilieva
Telerik team
answered on 12 Sep 2016, 12:17 PM
Hello Keith,

Thank you for the provided information and for the solutions in this scenario. I'm sure this information will help other users to implement this functionality.

Regards,
Maria Ilieva
Telerik by Progress
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
Grid
Asked by
Keith
Top achievements
Rank 1
Answers by
Keith
Top achievements
Rank 1
K'Library
Top achievements
Rank 1
Maria Ilieva
Telerik team
Share this question
or