Grid ajax-filtering and sortig problem

2 posts, 0 answers
  1. Henning
    Henning avatar
    2 posts
    Member since:
    Nov 2014

    Posted 30 Jun Link to this post

    Hi,

    I've a problem with filtering and sorting my grid. The Grid does not sort and filter my ViewModel-Properties.

    View:

    @(Html.Kendo().Grid<DepositModel>()
       .Name("gridClosedDeposits")
       .Columns(col =>
       {
         col.Bound(p => p.Id)
            .Title(Resources.Resource.ID);
         col.Bound(p => p.DateTime)
            .Format("{0:dd.MM.yyyy hh:mm}")
            .Title(Resources.Resource.Date);
         col.Bound(p => p.User_Id)
            .Title(Resources.Resource.CustomerId);
         col.Bound(p => p.User.FirstName)
            .Title(Resources.Resource.FirstName);
         col.Bound(p => p.User.LastName)
       })
       .Filterable()
       .Sortable()
       .Groupable()
       .Resizable(r =>
         r.Columns(true)
       )
       .Pageable(pageable => pageable
          .Refresh(true)
          .PageSizes(true)
          .ButtonCount(5))
       .DataSource(dataSource => dataSource
          .Ajax()
          .Sort(sort => sort.Add("DateTime").Descending())
       .Read(read => read.Action("GetClosedDeposits", "Grid"))
       .PageSize(10)
    )

    GetClosedDeposits-Function:

    public ActionResult GetOpenDeposits([DataSourceRequest]DataSourceRequest request)
    {
       using (var dbContext = new dbEntities())
       {
          IQueryable<Deposit> deposits =
             dbContext
                .Deposit
                   .Where(w =>
                      w.CanceledSmallDateTime == null &&
                      w.AccomplishedSmallDateTime == null);
     
          DataSourceResult result = deposits.ToDataSourceResult(request,
             s => new DepositModel
                  {
                     DateTime = s.DateTime,
                     Id = s.Id,
                     User_Id = s.User_Id,
                     User = new UserModel
                     {
                        Id = s.User1.Id,
                        FirstName = s.User1.FirstName.Split(' ')[0],
                        LastName = s.User1.LastName,
                        UserName = s.User1.UserName
                      }
                   }
          );
     
          return Json(result);
       }
    }

  2. Henning
    Henning avatar
    2 posts
    Member since:
    Nov 2014

    Posted 30 Jun Link to this post

    I've found my Problem.. Here the Solution...

    View:

    @(Html.Kendo().Grid<AdminDepositsDepositViewModel>()
       .Name("gridOpenDeposits")
       .Columns(col =>
       {
          col.Bound(p => p.Id)
             .Title(Resources.Resource.IdShort);
          col.Bound(p => p.DateTime)
             .Format("{0:dd.MM.yyyy hh:mm}")
             .Title(Resources.Resource.Date);
          col.Bound(p => p.User_Id)
             .Title(Resources.Resource.CustomerId);
          col.Bound(p => p.CustomerName)
             .Title(Resources.Resource.Customer);
          col.Bound(p => p.CustomerEMailAddress)
             .Title(Resources.Resource.EMailAddress);
       })
       .Filterable()
       .Sortable()
       .Resizable(r =>
          r.Columns(true)
       )
       .Pageable(pageable => pageable
          .Refresh(true)
          .PageSizes(true)
          .ButtonCount(5))
       .DataSource(dataSource => dataSource
          .Ajax()
          .Read(read => read.Action("GetOpenDeposits", "Grid"))
          .PageSize(10)
       )
    )

    Controller:

    public ActionResult GetOpenDeposits([DataSourceRequest]DataSourceRequest request)
    {
       using (var dbContext = new dbEntities())
       {
          IQueryable<AdminDepositsDepositViewModel> deposits =
             dbContext
                .Deposit
                   .Where(w =>
                      w.CanceledSmallDateTime == null &&
                      w.AccomplishedSmallDateTime == null)
                   .Select(s =>
                      new AdminDepositsDepositViewModel
                      {
                         CustomerEMailAddress = s.User1.UserName,
                         CustomerName = s.User1.FirstName + " " + s.User1.LastName,
                         DateTime = s.DateTime,
                         Id = s.Id,
                         User_Id = s.User_Id
                      }
                    );
     
          DataSourceResult result = deposits.ToDataSourceResult(request);
     
          return Json(result);
       }
    }

  3. UI for ASP.NET MVC is VS 2017 Ready
Back to Top