Use Filtered Results in MVC Controller

May 20, 2014 at 9:05 AM
First of all thank you for grid.mvc.

Is there any example of using the complete filtered result in an mvc controller after post

Thank you
Coordinator
May 31, 2014 at 2:52 PM
Edited May 31, 2014 at 2:54 PM
Hi,

Grid.Mvc contains filtered items in the Grid<T>.GridItems collection.

But problem to retrieve this items is that you need to configure your columns before retrieve. If grid columns configures in the VIEW you can't get it in the controller, because grid not configured yet.

You can configure your columns in the grid class:
    public class OrdersGrid : Grid<Order>
    {
        public OrdersGrid(IQueryable<Order> items)
            : base(items)
        {
            Columns.Add(x => x.OrderID)..Filterable(true);
            // other columns
        }
    }

controller:

        public ActionResult Grid()
        {
            var repository = new OrdersRepository();
            var grid = new OrdersGrid(repository.GetAll());
            return View(grid);
        }
In the view you can continue configuring the grid columns, using Get<T> mehtod of the columns collection:

@Html.Grid(Model).Named("ordersGrid").Columns(columns =>
{
        columns.Get(o => o.OrderID)
               .Titled("Number")
               .SetWidth(100);

})
After that you can retrieve filtered items using GridItems property:
        public ActionResult Post()
        {
            var repository = new OrdersRepository();
            var grid = new OrdersGrid(repository.GetAll());

            var filteredItems = grid.GridItems.ToList();
            return View();
        }
I have added Get<T> method to columns collection for this purpose. Please do not forget update the package - https://www.nuget.org/packages/Grid.Mvc/3.1.0-ci1030
Jul 17, 2014 at 11:20 AM
Edited Jul 17, 2014 at 11:23 AM
Hi Mathie,

If @Bukharin don't mind, why don't you follow this discussion..

Get the code from the following link

Compile the code. Get the DLL and add that DLL to your Project...

All credit only for Bukharin team.. I just modified it..
Jul 17, 2014 at 11:24 AM
Mistakenly posted. You've asked for something else, I've said for count.. Sorry Mathie..
Aug 13, 2014 at 7:29 AM
I have a problem if property is not from datebase.
Error:
The specified type member 'MantisTaskText' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.

public string MantisTaskText { get { return MantisTaskText = mantis_com.GetMantisTaskTextByID(this.MantisID); } set { } }
Jun 3, 2015 at 4:07 PM
I added the new package and Get does not exist in it.
Sep 8, 2016 at 1:09 PM
Bukharin wrote:
Hi,

Grid.Mvc contains filtered items in the Grid<T>.GridItems collection.

But problem to retrieve this items is that you need to configure your columns before retrieve. If grid columns configures in the VIEW you can't get it in the controller, because grid not configured yet.

You can configure your columns in the grid class:
    public class OrdersGrid : Grid<Order>
    {
        public OrdersGrid(IQueryable<Order> items)
            : base(items)
        {
            Columns.Add(x => x.OrderID)..Filterable(true);
            // other columns
        }
    }

controller:

        public ActionResult Grid()
        {
            var repository = new OrdersRepository();
            var grid = new OrdersGrid(repository.GetAll());
            return View(grid);
        }
In the view you can continue configuring the grid columns, using Get<T> mehtod of the columns collection:

@Html.Grid(Model).Named("ordersGrid").Columns(columns =>
{
        columns.Get(o => o.OrderID)
               .Titled("Number")
               .SetWidth(100);

})
After that you can retrieve filtered items using GridItems property:
        public ActionResult Post()
        {
            var repository = new OrdersRepository();
            var grid = new OrdersGrid(repository.GetAll());

            var filteredItems = grid.GridItems.ToList();
            return View();
        }
I have added Get<T> method to columns collection for this purpose. Please do not forget update the package - https://www.nuget.org/packages/Grid.Mvc/3.1.0-ci1030
Hi Bukharin,

I am trying to use the above code to get the filtered results in my controller, but I always get the full collection of items. I think it is because in the Post method you are getting all the items from the repository and passing those to a new instance of OrdersGrid in the constructor.
How can I create the new instance just with the results that have been filtered?