Filtering when using a ViewModel

5 posts, 0 answers
  1. Keith
    Keith avatar
    9 posts
    Member since:
    Jan 2014

    Posted 27 Jan 2014 Link to this post

    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. 









  2. Keith
    Keith avatar
    9 posts
    Member since:
    Jan 2014

    Posted 30 Jan 2014 in reply to Keith Link to this post

    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);
  3. UI for ASP.NET MVC is VS 2017 Ready
  4. K'Library
    K'Library avatar
    4 posts
    Member since:
    Aug 2015

    Posted 08 Sep Link to this post

    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.

     

  5. K'Library
    K'Library avatar
    4 posts
    Member since:
    Aug 2015

    Posted 08 Sep Link to this post

    Another filtering sample thread:

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

  6. Maria Ilieva
    Admin
    Maria Ilieva avatar
    4017 posts

    Posted 12 Sep Link to this post

    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
Back to Top
UI for ASP.NET MVC is VS 2017 Ready