tags:

views:

47

answers:

1

Hi all

Just beginning my journey in ASP.Net MVC and I have a query about something before I dig myself in too deep.

I have a table, which is paged, and I have 2 controls above the table:

  • Dropdown that defines order of the results and apply button next to it
  • Textbox that defines a filter and apply button next to it

What I need to achieve is that if the user changes the order or adds a filter I fire of an AJAX call to my action like such: /Membership/Users?sort=value&filter=value&page=pagenumber. So my controller action is:

   // GET Membership/Users?sort=&filter=&page=
   public ActionResult Users(string sort, string filter, string page)

So I have 3 questions:

  • Is this the correct approach?
  • What would be the best way to ensure that the query string is maintained, bearing in mind that the action will nearly always be called by Jquery/Ajax functions?
  • If I wanted to link directly to this action passing the arguments would I need to hard-code the querystring?

Thanks

+2  A: 

You could define a new route in the format Membership/Users/{sort}/{filter}/{page}.

routes.MapRoute(
        "MembershipList",                                              
        "Membership/Users/{sort}/{filter}/{page}",             
        new { controller = "Membership", action = "Users", sort = "", filter = "", page = "" }
    );

However, if the parameters are optional then I would suggest you leave it as is and don't define a route.

As you are passing through strings then they will simply be passed as null if for some reason no query strings are passed, your action should handle this and still render a view.

David Neale
David, that's good thanks. Any thoughts on my 2nd question? :-)
Mantorok
Sorry, I glanced over the rest. I'd say that it's impossible to properly couple-up your client-side calls to web services. You could define your own class in jQuery to hold the data but I'd say you'd need to be using this Action a lot to make it worth it.Essentially you're going to have to hard-code the URL with query strings or in a format that the MVC routing engine will recognise, if you're always going to pass those parameters and using the route more than once in the application then I'd suggest using the route.
David Neale