在 MVC 2 中的 Html.ActionLink 中传递字符串

发布于 2024-10-10 21:55:46 字数 1172 浏览 7 评论 0原文

我已经完成了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌生 2024-10-17 21:55:46

您似乎使用了错误的方法签名,我认为您正在使用这个< /a>

你想做

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

或者你可以做

Html.RouteLink(user.UserName, "AdminPassword", 
    new { username = user.UserName });

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 });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文