0

I'm having trouble with one particular issue, I was hoping someone could help me out.

I've completed the MVC Music Store tutorial, and now I'm trying to add some administrator functionality - practice as I will have to do this in an MVC application in my job. The application is using the aspnet membership api, and what I have done so far is created a view to list the users.

What I want to be able to do, is click on the users name in order to change their password. To try and carry the username to the changeUserPassword controller (custom made). I registered a new route in the global.asax.cs file in order to display the username in the URL, which is working so far.

UserList View

<%: Html.RouteLink(user.UserName, "AdminPassword", new { controller="StoreManager", action="changeUserPassword", username = user.UserName }) %>

Global.asax.cs

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

So now the URL looks like this when I reach the changeUserPassword view:

http://localhost:51236/StoreManager/changeUserPassword/Administrator

Here is the GET changeUserPassword action:

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


        return View();
    }

I wanted to store the username in ViewData as I would like to use it in the GET changeUserPassword for display purposes, and also as a hidden value in the form. This is in order to pass it through to enable me to reset the password.

Having debugged through the code, it seems that 'username' is null.

How can I get this to work so that the username carries over from the Html.RouteLink, to the changeUserPassword action?

Any help would be appreciated :)

Here is my complete code: UserList.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<System.Web.Security.MembershipUserCollection>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    UserList
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>UserList</h2>

    <table>
       <tr>
            <th>User Name</th>
            <th>Last Activity date</th>
            <th>Locked Out</th>
       </tr>
       <%foreach (MembershipUser user in Model){ %>


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


       <% }%>
    </table>


</asp:Content>

changeUserPassword.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<musicStoreMVC.ViewModels.ResetPasswordAdmin>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    changeUserPassword
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>Change Password: <%: ViewData["username"] %></h2>

    <% using (Html.BeginForm()) {%>
        <%: Html.ValidationSummary(true) %>

        <fieldset>
            <legend>Fields</legend>


            <div class="editor-label">
                <%: Html.Hidden("username",ViewData["username"]) %>
                <%: Html.LabelFor(model => model.password) %>
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.password) %>
                <%: Html.ValidationMessageFor(model => model.password) %>
            </div>

            <div class="editor-label">
                <%: Html.LabelFor(model => model.confirmPassword) %>
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.confirmPassword) %>
                <%: Html.ValidationMessageFor(model => model.confirmPassword) %>
            </div>

            <p>
                <input type="submit" value="Create" />
            </p>
        </fieldset>

    <% } %>

    <div>
        <%: Html.ActionLink("Back to List", "Index") %>
    </div>

</asp:Content>

My actions

 public ActionResult UserList()
        {
            var users = Membership.GetAllUsers();

            return View(users);
        }

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


            return View();
        }
5
  • What other routes do you have? Commented Jan 9, 2011 at 15:08
  • just the route posted above, and the default route. Commented Jan 9, 2011 at 15:10
  • Your code is working fine here :)? Commented Jan 9, 2011 at 15:57
  • Hmm. It's a strange one. In MartinHN's answer below, the URL he posted works fine, but clicking on the link does not generate that URL, which I think is my problem. Out of curiosity, how does the URL appear for you? Commented Jan 9, 2011 at 16:39
  • actually, scrap that. Once I put back in the controller, action back into the Route it worked perfectly. Thanks so much for your help :) Commented Jan 9, 2011 at 16:41

1 Answer 1

1

Something must be going wrong with your routes.

If you navigate to this URL, and set a breakpoint in the changeUserPassword action method - you'll probable see the username value correctly:

http://localhost:51236/StoreManager/changeUserPassword?username=Administrator

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

12 Comments

@MartinHN, navigating using that URL I can see the username perfectly. Any ideas as to how I can solve this issue with the routes then?
You can try changing the route as follows: routes.MapRoute("AdminPassword", "StoreManager/changeUserPassword/{username}", new { username = UrlParameter.Optional});
Hi, I've changed the route as you mentioned above, however now the URL displays as this "localhost:51236/StoreManager/changeUserPassword/…" when I click on the link. Any ideas?? I also tried removing everything but 'username = user.UserName' from the routelink, however that did not work either.
Have you also changed your routelink to this: <%: Html.RouteLink(user.UserName, "AdminPassword", new { username = user.UserName }) %> ?
Make sure that you map the AdminPassword route before the DefaultRoute in the Global.asax. (Moved to a comment, because it needs to be done in conjunction with MartinHN's answer).
|

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.