MVC 控制器帮助:将字符串从视图传递到控制器
我在一个特定问题上遇到了麻烦,我希望有人可以帮助我。
我已经完成了 MVC 音乐商店教程,现在我正在尝试添加一些管理员功能 - 练习一下,因为我必须在工作中的 MVC 应用程序中执行此操作。该应用程序正在使用 aspnet 会员 api,到目前为止我所做的是创建一个视图来列出用户。
我想要做的是单击用户名以更改其密码。尝试将用户名传递到changeUserPassword控制器(定制)。我在 global.asax.cs 文件中注册了一条新路由,以便在 URL 中显示用户名,到目前为止该路由有效。
UserList 视图
<%: 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}
);
现在,当我到达changeUserPassword 视图时,URL 如下所示:
http://localhost:51236/StoreManager/changeUserPassword/Administrator
这是 GET changeUserPassword 操作:
public ActionResult changeUserPassword(string username)
{
ViewData["username"] = username;
return View();
}
I想要将用户名存储在 ViewData 中,因为我想在 GET ChangeUserPassword 中使用它以用于显示目的,并作为表单中的隐藏值。这是为了让我能够重置密码。
通过代码调试后,似乎“用户名”为空。
我怎样才能让它工作,以便用户名从 Html.RouteLink 转移到 ChangeUserPassword 操作?
任何帮助将不胜感激:)
这是我的完整代码: 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>
我的操作
public ActionResult UserList()
{
var users = Membership.GetAllUsers();
return View(users);
}
public ActionResult changeUserPassword(string username)
{
ViewData["username"] = username;
return View();
}
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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的路线肯定出了问题。
如果您导航到此 URL,并在 ChangeUserPassword 操作方法中设置断点 - 您可能会正确看到用户名值:
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: