0

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
3
  • How do you end up with that URL? Commented May 4, 2012 at 12:47
  • Huh? What are you asking about? What did that URL come from? Commented 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 }) Commented May 4, 2012 at 12:53

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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
My mistake mate, I tried it again but added the name of the action and controller, thanks very much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.