is there any way to pass parameters to mvc like this controller/action?id=15 instead of controller/action/15?
I think mvc should know how to handle this, and I've seen some examples of people giving vague hints about this sort of usage, but I've failed miserably to get it to work.
Any ideas where I might start or what I might be doing wrong?
(the only thing that I think might be interfering is the fact that I'm using mvc on iss6 so the default routing setup is changed to use the .mvc file name)
Yes you can use query string parameters in asp.net mvc. The framework automatically passes those paramters to your action method. The trick is to make sure the input parameters for your action method have the exact same variable name as the querystring
parameter.
For example if your url looks like this controller/someaction?name=bob. The corresponding action method would look like this
public ActionResult someaction(string name){
return View();
}
The framework automatically maps querystring and form parameters to action method input parameters for you (once again, as long as the names are identical). I know this works with IIS6 because I have a project running on iis6 (complete with .mvc file extension)
and I can even pass multiple parameters via querystring(though I try to avoid this).
Ok, so I made a test app to see if I screwed the pooch in mine.
public ActionResult Test(int id, string name)
{
return View("Index");
}
called it with home/test?id=1
and got back
The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Test(Int32, System.String)' in 'MvcApplication2.Controllers.HomeController'. To make a parameter optional its
type should be either a reference type or a Nullable type.
Parameter name: parameters
"id" is already a special value that you're getting from your route, and I believe route values are preferred over query string values (not sure, I'd have to double check on that, but the error you're getting sure makes it sound that way).
Marked as answer by linkerro on Jun 16, 2009 06:13 AM
Hmm I just tried it and got the same message. I believe it's a naming issue that has to do with the fact that the default route extracts the parameter "Id" from the url. So even if you pass Id in via querystring it's going to set the Id field to null because
of how the route is setup. Instead of passing the Id as a querystring pass it using a clean url then you can append the rest of the parameters as querystring parameters. I just tried this and it worked. I called /home/test/2?name=test
public ActionResult Test(int id,string name){
return View("Index"); // In the debugger I found id=2 , name=test
I then changed the action method and tried this /home/test/2?firstname=bob&lastname=smith to make sure all the querystrings automatically get binded and surely enough it had the correct value in the debugger.
public ActionResult Test(int id,string firstname,string lastname){ return View("Index"); // In the debugger I found id=2 , firstname=bob, lastname=smith
}
Another thing you can do is in your action method don't call the Id field "Id" (because it conflicts with the route) call it sid or something. This just worked for me calling home/test/?sid=1&name=test with this action method
public ActionResult Test(int sid,string name){ return View("Index");// in the debugger sid =1 & name=test
linkerro
Member
3 Points
8 Posts
Url encoded parameters in mvc
Jun 15, 2009 02:49 PM|LINK
is there any way to pass parameters to mvc like this controller/action?id=15 instead of controller/action/15?
I think mvc should know how to handle this, and I've seen some examples of people giving vague hints about this sort of usage, but I've failed miserably to get it to work.
Any ideas where I might start or what I might be doing wrong?
(the only thing that I think might be interfering is the fact that I'm using mvc on iss6 so the default routing setup is changed to use the .mvc file name)
"ASP.NET MVC 1.0" "ASP.NET MVC v1.0"
CodeHobo
All-Star
18647 Points
2647 Posts
Re: Url encoded parameters in mvc
Jun 15, 2009 04:25 PM|LINK
Yes you can use query string parameters in asp.net mvc. The framework automatically passes those paramters to your action method. The trick is to make sure the input parameters for your action method have the exact same variable name as the querystring parameter.
For example if your url looks like this controller/someaction?name=bob. The corresponding action method would look like this
public ActionResult someaction(string name){ return View();The framework automatically maps querystring and form parameters to action method input parameters for you (once again, as long as the names are identical). I know this works with IIS6 because I have a project running on iis6 (complete with .mvc file extension) and I can even pass multiple parameters via querystring(though I try to avoid this).
Blog | Twitter : @Hattan
linkerro
Member
3 Points
8 Posts
Re: Url encoded parameters in mvc
Jun 16, 2009 05:41 AM|LINK
Ok, so I made a test app to see if I screwed the pooch in mine.
public ActionResult Test(int id, string name) { return View("Index"); }called it with home/test?id=1
and got back
The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Test(Int32, System.String)' in 'MvcApplication2.Controllers.HomeController'. To make a parameter optional its type should be either a reference type or a Nullable type.
Parameter name: parameters
bradwils
Contributor
5779 Points
691 Posts
Microsoft
Re: Url encoded parameters in mvc
Jun 16, 2009 06:03 AM|LINK
"id" is already a special value that you're getting from your route, and I believe route values are preferred over query string values (not sure, I'd have to double check on that, but the error you're getting sure makes it sound that way).
CodeHobo
All-Star
18647 Points
2647 Posts
Re: Url encoded parameters in mvc
Jun 16, 2009 06:05 AM|LINK
Hmm I just tried it and got the same message. I believe it's a naming issue that has to do with the fact that the default route extracts the parameter "Id" from the url. So even if you pass Id in via querystring it's going to set the Id field to null because of how the route is setup. Instead of passing the Id as a querystring pass it using a clean url then you can append the rest of the parameters as querystring parameters. I just tried this and it worked. I called /home/test/2?name=test
public ActionResult Test(int id,string name){ return View("Index"); // In the debugger I found id=2 , name=testI then changed the action method and tried this /home/test/2?firstname=bob&lastname=smith to make sure all the querystrings automatically get binded and surely enough it had the correct value in the debugger.
Another thing you can do is in your action method don't call the Id field "Id" (because it conflicts with the route) call it sid or something. This just worked for me calling home/test/?sid=1&name=test with this action method
Blog | Twitter : @Hattan
linkerro
Member
3 Points
8 Posts
Re: Url encoded parameters in mvc
Jun 16, 2009 06:14 AM|LINK
Yup, that solved it. Thanks a ton guys.