Hi guys,
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();
}