0

Setup:

I am using the Html.ActionLink helper to create a tags to my action, Reset, in the controller PasswordReset, in the area Admin.

Html.ActionLink calls (EDITED, but still no joy):

The calls to Html.ActionLink are in a foreach loop (html simplified):

foreach(var item in Model.Entities)
{
   <p>
      @Html.ActionLink("Reset Password", 
          "Reset",
          "PasswordReset", 
          new {area="Admin", 
               userName=item.UserName, 
               email=item.Email, 
               roles=item.Roles},
          null)
   </p>
}

My action:

[HttpGet]
        public virtual ActionResult Reset(string userName, string email, string roles)
        {
            if (string.IsNullOrEmpty(userName)) throw new ApplicationException("Invalid Username!");
            var ue = new UsernameEmailDTO
            {
                UserName = userName,
                Email = email,
                Roles = roles
            };
            return View(ue);
        }

Route:

context.MapRoute(
            "Admin_PasswordReset",
            "Admin/Password/Reset/{userName}",
            new { controller = "PasswordReset", action="Reset", email = UrlParameter.Optional, roles = UrlParameter.Optional }
        );

Problem:

I can call the action directly using the route.

However, my action link just produces a link that points to the current page.

So there has to be an error in my Html.ActionLink code.

But what?!

Very frustrating as I thought I was over this variety of pain barrier.

awrigley
  • 13,481
  • 10
  • 83
  • 129
  • Shooting from the hip, but try moving the controller property out of the routeValues and into its native parameter of the ActionLink helper. – Moby's Stunt Double Apr 11 '13 at 17:38
  • @Moby'sStuntDouble: Nope, doesn't work either. I have however amended the question with your suggestion. – awrigley Apr 11 '13 at 17:43
  • Could you post the link produced by the Html.ActionLink? – Fals Apr 11 '13 at 19:00
  • As stated in the question, it just produces a link that points to the current page. No point posting that. – awrigley Apr 11 '13 at 19:12
  • Well just trying to help you! Maybe something is messed up by Areas configuration! Maybe Html.RouteLink solves the problem. Take a look here! http://stackoverflow.com/questions/5432185/html-actionlink-between-areas – Fals Apr 11 '13 at 19:27
  • Thanks your effort, Fals. Sorry if I have been snappy about you guessing. I'm call it a day. – awrigley Apr 11 '13 at 19:31
  • Have you tried Haack's Route Debugger? http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx – Moby's Stunt Double Apr 11 '13 at 20:41

1 Answers1

0

The helper is overridding your url with the current route path. Pass routeValues: null as parameter to resolve this issue.

foreach(var item in Model.Entities)
{
    <p>
     @Html.ActionLink("Reset Password", "Reset", "PasswordReset", 
     new {
            userName = item.UserName,
            email = item.Email,
        roles = item.Roles }
    )
    </p>
}

EDIT: I see this now. The problem is your route config. You are passing 3 parameters, but your routes take only one. When Html helper creates the route, its take the first route that matches.

context.MapRoute(
        "Admin_PasswordReset",
        "Admin/Password/Reset/{userName}/{email}/{roles}",
        new { controller = "PasswordReset", action="Reset", email = UrlParameter.Optional, roles = UrlParameter.Optional }
    );

Found it here: Html ActionLink Route problem

Community
  • 1
  • 1
Fals
  • 6,813
  • 4
  • 23
  • 43
  • you can't set routeValues to null - I need that! Did you mean htmlAttributes = null? Also, as written, your code produces an error: named parameters can't go before positional ones. – awrigley Apr 11 '13 at 17:54
  • I did some research try this and let me know! :) – Fals Apr 11 '13 at 18:25
  • there is no reason for it work if my route doesn't: the other two parameters are optional. As in UrlParameter.Optional. I tried it and as expected, it doesn't work. – awrigley Apr 11 '13 at 18:52