6

I'll try to make this as concise as possible.

  • Webpage contains a table that allows for filtering and sorting
  • Changes to filtering and sorting should be reflected in the URL so the user can bookmark or share filtered views.

The question is: What is an effective convention of allowing all of the sort and filter syntax to be part of the URL and easily interpret/use it on the server without having to write a bunch of custom code that interprets it?

I've been doing some research and I came across the OData URI conventions and I like the way they do things. http://www.odata.org/developers/protocols/uri-conventions

More research shows the the MVC 4 Web API allows for use of that convention by returning an IQueryable. This looks fantastic except for one part... I'm not implementing a RESTful API at this point and that's all it seems to work with. So how can I use something like OData and still return a View or PartialView? Is there something that will parse the OData URI convention into a C# object?

If anyone has any insights into this problem or suggestions, I'm all ears.

tereško
  • 58,060
  • 25
  • 98
  • 150
Zifik
  • 96
  • 1
  • 5
  • When you say MVC 4 Web Api, do you mean Asp.Net's? If so, the url parameters are automatically passed to action methods, and will try and be interpreted as the type you declare them as in the action method's signature. – Chris Carew Mar 22 '12 at 22:56
  • Yes, thus us about ASP.NET MVC and yes I know how parameters work with action methods. The problem is that when you get into putting complex filters into the URL, things get hairy. I was hoping for a convention/framework that handles it for me. MVC 4's web API is really close to handling it for me, but it's focused on data API and I need regular Views and PartialVies – Zifik Mar 23 '12 at 13:54
  • How are you generating your list of filters in the first place? Are they stored in a database? Dynamically generated from the data set? Hardcoded into the page? – Bobson May 03 '12 at 11:22
  • Telerik Grid for MVC uses something similar. It is an open source project, take a look at their code: http://www.telerik.com/products/aspnet-mvc/grid.aspx http://telerikaspnetmvc.codeplex.com/ – LeffeBrune Aug 30 '12 at 13:26

2 Answers2

0

As for the url convention part of your question, I think you have answered your own question with OData. As for getting this data into a C# object I would use the following approach:

Use an action filter to interperet the url parameters and parse them into a c# object. In your action filter add the url parameters to the route data and the c# object will be available in your action.

ASP.NET MVC Pass object from Custom Action Filter to Action

Take a look at the Telerik MVC grid, they use a GridAction action filter that does pretty much what you are asking.

Community
  • 1
  • 1
nixon
  • 1,952
  • 5
  • 21
  • 41
0

I would look at custom model binding. A good overview can be found here: http://weblogs.asp.net/nmarun/archive/2010/02/25/asp-net-mvc-model-binding.aspx

It's typically used for POST requests with forms but there's no reason why you can't use it for GET requests too.

Basically, your approach should be to:

  1. Create a new Model class with your filter/sorting parameters as properties:

    public class TableParameters {
         public string TableFilter { get; set; }
    }
    
  2. In your Controller's Action, add the model as a parameter

     public ActionResult TableAction(TableParameters parameters) { /* Action logic */ }
    
  3. Set your parameters in the URL by saying:

     /Controller/TableAction?TableFilter=[your-filter-string]
    

The parameters object in your action will have the property populated with the value from the query string.

infl3x
  • 1,492
  • 19
  • 26
  • Also worth being aware of the security side of model binding http://odetocode.com/blogs/scott/archive/2012/03/11/complete-guide-to-mass-assignment-in-asp-net-mvc.aspx – infl3x Aug 15 '12 at 00:16