在 MVC 2 中的 Html.ActionLink 中传递字符串
我已经完成了 MVC Music Store 应用程序,现在正在进行自己的修改以进行练习,并为我在工作中执行类似的任务做好准备。
我所做的是创建一个显示当前用户列表的视图。我这样做是因为我希望能够更改用户忘记的密码。这是我的观点的片段:
<% 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>
<% }%>
我想知道的是,是否可以像我上面所做的那样通过操作链接传递用户名。我已在 global.asax.cs 文件中注册了以下路由,但是我不确定是否正确完成:
routes.MapRoute(
"AdminPassword", //Route name
"{controller}/{action}/{username}", //URL with parameters
new {
controller = "StoreManager",
action = "changeUserPassword",
username = UrlParameter.Optional
});
这是我的 GET 操作:
public ActionResult changeUserPassword(string username)
{
ViewData["username"] = username;
return View();
}
我已通过代码进行调试,发现 ViewData["username"] = username 没有没有填充,所以看起来要么我没有正确注册路由,要么我根本无法像我一样使用操作链接传递用户名。
如果有人能指出我正确的方向,我将不胜感激。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您似乎使用了错误的方法签名,我认为您正在使用这个< /a>
你想做
或者你可以做
You seem to be using the wrong method signature, I think you're using this one
You want to do
Or you can do