Nearly every example I've seen shows the exact same pattern: /Controller/Method/Parameter.
What I would like to know is how to send multiple parameters, just like is normal with methods. Perhaps I want to send the id and a few other pieces of dynamic information along to the Controller method. I believe that I can add those into a routeValues
object, so long as they're a part of the current Model, but what if I want to pass completely arbitrary information into the method? Some of the parameters I need to pass aren't a part of the Model. I tried adding new entry to the Global.asax file, but this
didn't help any.
It's not how hard you push in life, but who you push, that makes the difference between success and running for your life.
I'm not sure if this is what you are tring to do, but you can send any serializable object as a query (get) variable.
For example: /Login/AddUser/10?name=John&last=Doe
would pass int id = 10, string name = "John", and string last="Doe" into the method AddUser.
Also, when creating an ActionLink, the routeValues can also do this. IE Html.ActionLink("Display", "AddUser", "Login", new{ id=10, name="John", last="Doe"}, new{})
Generally extra values are passed as part of the form or query string. For example, if your query string is ?foo=fooValue&bar=42, you can write your action as so:
public ActionResult MyAction(..., string foo, int bar) { }
You can also use a combination of routes and query string or form values. So if your route is {controller}/{action}/{id}/{page}, then /Blog/View/20091203001/2?showComments=true, then you could pull this in as:
public ActionResult View(string id /* = "20091203001" */, int page /* = 2 */, bool showComments /* = true */) { }
So if I'm understanding this correctly, sending a request with more than one parameter requires the use of a querystring? What if I wanted to type in a url for a blog post that filtered for year, month and day like:
So if I'm understanding this correctly, sending a request with more than one parameter requires the use of a querystring?
Not at all. In fact, my post (just like ali62b's) showed an example of a route with more complicated pattern matching. If you want, you can shove everything into the route and leave off the query string entirely. But remember that a URL is a
resource. We created Routing so that it more properly describes your site's resources, not so that you never have query strings in your applications. As an example, parameters that affect the presentation but not the actual resource itself (such
as my example's showComments parameter) should generally be left as query strings rather than as route values.
I agree with keeping the presentation out of the url. Actually, the thought to do so had never occured to me. I'm simply wanting to use the url to filter results.
It's not how hard you push in life, but who you push, that makes the difference between success and running for your life.
WilliamSnell
Member
351 Points
550 Posts
Sending multiple parameters to Controller methods
Dec 03, 2009 05:42 PM|LINK
Nearly every example I've seen shows the exact same pattern: /Controller/Method/Parameter.
What I would like to know is how to send multiple parameters, just like is normal with methods. Perhaps I want to send the id and a few other pieces of dynamic information along to the Controller method. I believe that I can add those into a routeValues object, so long as they're a part of the current Model, but what if I want to pass completely arbitrary information into the method? Some of the parameters I need to pass aren't a part of the Model. I tried adding new entry to the Global.asax file, but this didn't help any.
joecoolish
Member
4 Points
18 Posts
Re: Sending multiple parameters to Controller methods
Dec 03, 2009 06:48 PM|LINK
I'm not sure if this is what you are tring to do, but you can send any serializable object as a query (get) variable.
For example: /Login/AddUser/10?name=John&last=Doe
would pass int id = 10, string name = "John", and string last="Doe" into the method AddUser.
Also, when creating an ActionLink, the routeValues can also do this. IE Html.ActionLink("Display", "AddUser", "Login", new{ id=10, name="John", last="Doe"}, new{})
Hope this helps!
ali62b
Contributor
4750 Points
690 Posts
Re: Sending multiple parameters to Controller methods
Dec 03, 2009 07:01 PM|LINK
controller code :
public ActionResult ParameterTest(int a, int b, int c) { ViewData["result"] = a + b + c; return View(); }global.asax.cs:
public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Parameter", "{controller}/{action}/{a}/{b}/{c}", new { controller = "Home", action = "ParameterTest", a = 0, b = 0, c = 0 } ); }and in view :
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>ParameterTest</title> </head> <body> <div> <%=ViewData["result"] %> </div> </body> </html>hope this helps
ASP.NET MVC parameters
@BlueCoder
Regards
levib
Star
7702 Points
1099 Posts
Microsoft
Re: Sending multiple parameters to Controller methods
Dec 03, 2009 07:03 PM|LINK
Generally extra values are passed as part of the form or query string. For example, if your query string is ?foo=fooValue&bar=42, you can write your action as so:
public ActionResult MyAction(..., string foo, int bar) { }
You can also use a combination of routes and query string or form values. So if your route is {controller}/{action}/{id}/{page}, then /Blog/View/20091203001/2?showComments=true, then you could pull this in as:
public ActionResult View(string id /* = "20091203001" */, int page /* = 2 */, bool showComments /* = true */) { }
WilliamSnell
Member
351 Points
550 Posts
Re: Sending multiple parameters to Controller methods
Dec 07, 2009 05:22 PM|LINK
So if I'm understanding this correctly, sending a request with more than one parameter requires the use of a querystring? What if I wanted to type in a url for a blog post that filtered for year, month and day like:
http://www.somewhere.com/blog/2009/12/3
Would I instead have to do this:
http://www.somewhere.com/blog/2009?month=12&day=3
That just breaks most of the idea behind the url routing, I would think. I'm going to test "ali62b"s suggestion. Hopefully that will work.
levib
Star
7702 Points
1099 Posts
Microsoft
Re: Sending multiple parameters to Controller methods
Dec 07, 2009 05:46 PM|LINK
Not at all. In fact, my post (just like ali62b's) showed an example of a route with more complicated pattern matching. If you want, you can shove everything into the route and leave off the query string entirely. But remember that a URL is a resource. We created Routing so that it more properly describes your site's resources, not so that you never have query strings in your applications. As an example, parameters that affect the presentation but not the actual resource itself (such as my example's showComments parameter) should generally be left as query strings rather than as route values.
WilliamSnell
Member
351 Points
550 Posts
Re: Sending multiple parameters to Controller methods
Dec 07, 2009 07:24 PM|LINK
I agree with keeping the presentation out of the url. Actually, the thought to do so had never occured to me. I'm simply wanting to use the url to filter results.