【问题标题】:@Html.ActionLink helper pointing to current page@Html.ActionLink 帮助器指向当前页面
【发布时间】:2013-04-11 17:32:02
【问题描述】:

设置:

我正在使用Html.ActionLink 帮助器在PasswordReset 控制器区域Admin 中为我的操作创建标签Reset

Html.ActionLink 调用(已编辑,但仍然没有乐趣):

对 Html.ActionLink 的调用在 foreach 循环中(html 简化):

foreach(var item in Model.Entities)
{
   <p>
      @Html.ActionLink("Reset Password", 
          "Reset",
          "PasswordReset", 
          new {area="Admin", 
               userName=item.UserName, 
               email=item.Email, 
               roles=item.Roles},
          null)
   </p>
}

我的行动:

[HttpGet]
        public virtual ActionResult Reset(string userName, string email, string roles)
        {
            if (string.IsNullOrEmpty(userName)) throw new ApplicationException("Invalid Username!");
            var ue = new UsernameEmailDTO
            {
                UserName = userName,
                Email = email,
                Roles = roles
            };
            return View(ue);
        }

路线:

context.MapRoute(
            "Admin_PasswordReset",
            "Admin/Password/Reset/{userName}",
            new { controller = "PasswordReset", action="Reset", email = UrlParameter.Optional, roles = UrlParameter.Optional }
        );

问题:

我可以直接使用路由调用动作。

但是,我的操作链接只生成一个指向当前页面的链接。

所以我的 Html.ActionLink 代码中一定有错误。

但是什么?!

非常沮丧,因为我以为我已经克服了这种疼痛障碍。

【问题讨论】:

  • 从臀部射击,但尝试将控制器属性移出 routeValues 并移入 ActionLink 帮助器的本机参数。
  • @Moby'sStuntDouble:不,也不起作用。但是,我已根据您的建议修改了问题。
  • 你能发布由 Html.ActionLink 生成的链接吗?
  • 如问题所述,它只生成一个指向当前页面的链接。没有必要张贴。
  • 只是想帮助你!可能是区域配置搞砸了!也许 Html.RouteLink 解决了这个问题。看看这里! stackoverflow.com/questions/5432185/…

标签: asp.net-mvc asp.net-mvc-4 html.actionlink


【解决方案1】:

助手正在用当前路由路径覆盖您的 url。传递 routeValues: null 作为参数来解决这个问题。

foreach(var item in Model.Entities)
{
    <p>
     @Html.ActionLink("Reset Password", "Reset", "PasswordReset", 
     new {
            userName = item.UserName,
            email = item.Email,
        roles = item.Roles }
    )
    </p>
}

编辑:我现在看到了。问题是您的路线配置。您正在传递 3 个参数,但您的路线只需要一个。当 Html 助手创建路由时,它会采用第一个匹配的路由。

context.MapRoute(
        "Admin_PasswordReset",
        "Admin/Password/Reset/{userName}/{email}/{roles}",
        new { controller = "PasswordReset", action="Reset", email = UrlParameter.Optional, roles = UrlParameter.Optional }
    );

在这里找到它:Html ActionLink Route problem

【讨论】:

  • 您不能将 routeValues 设置为 null - 我需要它!您的意思是 htmlAttributes = null 吗?此外,如所写,您的代码会产生错误:命名参数不能放在位置参数之前。
  • 我做了一些研究,试试这个,让我知道! :)
  • 如果我的路线不正常,它就没有理由工作:其他两个参数是可选的。如在 UrlParameter.Optional 中。我试过了,和预期的一样,它不起作用。
猜你喜欢
  • 1970-01-01
  • 2011-03-20
  • 1970-01-01
  • 1970-01-01
  • 2018-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-27
相关资源
最近更新 更多