0

I have completed the MVC Music Store application, and now am making my own modifications for practice, and also to prepare me to do similar tasks in my job.

What I have done, is create a view that displays the current list of users. I have done this as I want to be able to change the passwords of users should they forget them. Here is a snippet from my view:

<% foreach (MembershipUser user in Model) { %>

<tr>
    <td><%: Html.ActionLink(user.UserName, "changeUserPassword", 
        "StoreManager", new { username = user.UserName }) %></td>
    <td><%: user.LastActivityDate %></td>
    <td><%: user.IsLockedOut %></td>
</tr>

<% }%>

What I want to know, is it possible to pass the username through the actionlink as I have done above. I have registered the following route in the global.asax.cs file, however I am unsure if I have done it correctly:

routes.MapRoute(
    "AdminPassword", //Route name
    "{controller}/{action}/{username}", //URL with parameters
    new { 
        controller = "StoreManager", 
        action = "changeUserPassword", 
        username = UrlParameter.Optional
    });

Here is my GET action:

public ActionResult changeUserPassword(string username)
{
    ViewData["username"] = username;

    return View();
}

I have debugged through the code to find that ViewData["username"] = username doesn't populate so it looks like either I have not registered the routes properly, or I simply cannot pass the username using the actionlink like I have.

I'd be grateful if someone could point me in the right direction.

1 Answer 1

2

You seem to be using the wrong method signature, I think you're using this one

You want to do

Html.ActionLink(user.UserName, "changeUserPassword", 
    new { controller = "StoreManager", username = user.UserName });

Or you can do

Html.RouteLink(user.UserName, "AdminPassword", 
    new { username = user.UserName });
Sign up to request clarification or add additional context in comments.

Comments

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.