Reputation: 12855
I use the Url.Action method to generate a Url string.
Thats the result:
"/Home/GetRejectTest/0?IsSelected=False"
The controller and action name are correct but the query parameters are screwed up. Is this because The action does not have a RouteAttribute thus query parameters are generated?
My action:
public ActionResult GetRejectTest(Test test)
{
return new EmptyResult();
}
Test class has those 3 properties Id, Name, IsSelected.
My route definition:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Upvotes: 1
Views: 3146
Reputation:
The url your generating (/Home/GetRejectTest/0?IsSelected=False
) is correct for your route definition. You passing a new instance of Test
to the Url.Action()
method, which
.ToString()
) of each property in your model - i.e.
controller=Home, action=GetRejectTest, id=0, Name=null, IsSelected=False
url: "{controller}/{action}/{id}"
) and updates the placeholders
(which at this point generates /Home/GetRejectTest/0
) but your
route definition does not have url parameters for Name
and
IsSelected
so these are added as query string parameters (because
Name
is null
, a query string for that propery is not generated)
so the result is now /Home/GetRejectTest/0?IsSelected=False
You have not indicated what result you're actually expecting, but creating specific route definitions will solve most cases. For example if you want
/Home/GetRejectTest/0/false
or /Home/GetRejectTest/0/false/someName
if the value of Name
is not null
, then you can create an additional route (which must be before the default route)
routes.MapRoute(
name: "Test",
url: "Home/GetRejectTest/{id}/{isselected}/{name}",
defaults: new { controller = "Home", action = "GetRejectTest", name = UrlParameter.Optional }
);
Note that because Name
is typeof string
and therefore can be null
, the {name}
placeholder needs be the last one and marked as UrlParameter.Optional
(otherwise it will revert back to using query string parameters)
Upvotes: 5