【问题标题】:Add #id suffix to RedirectToAction() in controller ASP NET MVC在控制器 ASP NET MVC 中将 #id 后缀添加到 RedirectToAction()
【发布时间】:2020-10-03 14:46:39
【问题描述】:

不知怎的,我觉得这很难描述,但我开始了:

我的 SelectClasses Razor 视图页面中有一个 id="id152" 的 div。 为了让我在重新加载时在页面上显示该 div,我必须将后缀 #id152 添加到我的页面 url。

<div id="id152">blabla</div>
...
..
<a href="#id152">Section 7</a>

现在我的问题是:有没有办法将此后缀添加/传递给“RedirectToAction()”?

public ActionResult Index()
{
    //All we want to do is redirect to the class selection page and add a suffix
    return RedirectToAction("SelectClasses", "Registration", new { id = 99 })); //add suffix here somewhere
}

所以当我的 SelectClasses 视图显示时,url 看起来像这样:

'[url]/SelectClasses/99#id152'

【问题讨论】:

  • 什么动作使页面重新加载?如果您没有提交任何表单,则页面无需重新加载页面。

标签: html asp.net-mvc redirecttoaction


【解决方案1】:

RedirectToActionResult(在其余的RedirectTo* 结果中)用于根据注册的路由数据生成 URL。

在您的情况下,您希望连接一个未发送到服务器且仅由浏览器使用的哈希参数值 (#id152)。这就是为什么说的方法不费心处理它的原因。

我建议你这样做:

var redirUrl = Url.Action("SelectClasses", "Registration", new { id = 99 });
redirUrl = String.Concat(redirUrl, "#id152");

return Redirect(redirUrl);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-21
    • 1970-01-01
    • 1970-01-01
    • 2017-03-23
    • 1970-01-01
    • 2015-09-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多