I've just started MVC and I can pass through an ID to a page but can't seem to get my routing to work with two parameters. Does anyone have any ideas why?
Here is my code:
Global:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Account", action = "Login", id = UrlParameter.Optional }
);
routes.MapRoute(
"EditVoucher", // Route name
"{controller}/{action}/{id}/{userid}", // URL with parameters
new { controller = "Admin", action = "EditVoucher", id = "", userid = "" } // Parameter defaults
);
**My controller:**
[HttpGet]
public ActionResult EditVoucher(int ID, int UserID)
{
}
**my link:**
@Html.ActionLink("[Edit]", "EditVoucher", new { Controller = "Admin", id = item.ID, userid = 2 })
this passes through the values fine but I end up with this sort of URL:
**/Admin/EditVoucher/2?userid=2**
thanks
asked May 4, 2012 at 12:45
Funky
13.7k37 gold badges110 silver badges166 bronze badges
-
How do you end up with that URL?SLaks– SLaks2012年05月04日 12:47:04 +00:00Commented May 4, 2012 at 12:47
-
Huh? What are you asking about? What did that URL come from?SLaks– SLaks2012年05月04日 12:50:28 +00:00Commented May 4, 2012 at 12:50
-
I have added it: here it is again: @Html.ActionLink("Edit", "EditVoucher", new { Controller = "Admin", id = item.ID, userid = 2 })Funky– Funky2012年05月04日 12:53:06 +00:00Commented May 4, 2012 at 12:53
1 Answer 1
ActionLink will use the first route that can satisfy your parameters.
Since the first (default) route also satisfies your parameters, you need to put the custom route first.
answered May 4, 2012 at 12:53
SLaks
891k182 gold badges1.9k silver badges2k bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
Funky
I already tried that and the following error when starting the site up: The parameters dictionary contains a null entry for parameter 'ID' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult EditVoucher(Int32, Int32)' in 'Vouchers.Admin.UI.Controllers.AdminController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters
Funky
My mistake mate, I tried it again but added the name of the action and controller, thanks very much!
lang-cs