开发者

Maintaining the query string in ASP.Net MVC

开发者 https://www.devze.com 2023-01-03 17:57 出处:网络
Just beginning my journey in ASP.Net MVC and I have a query about something before I dig myself in too deep.

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消