gpt4 book ai didi

javascript - 使用 onchange 重定向到另一个 Controller

转载 作者:行者123 更新时间:2023-11-28 20:20:59 24 4
gpt4 key购买 nike

<th>@Html.DropDownList("Tables", new List<SelectListItem>
{
new SelectListItem { Text = "A", Value = "Admin/Index"},
new SelectListItem { Text = "B", Value = "Admin/Index"}
}, "WWW",new { @onchange="location=this.value" })</th>

此代码仅适用于第一次,后来重定向到 Controller “Admin/Index”,它开始复制自身。所以我有这样的地址

localhost/Admin/Admin/Index instead of localhost/Admin/Index

我也试过了,但效果一样

@onchange="document.href.location=this.value
@onchange="window.href.location=this.value

如何制作正确的js,以便重定向始终有效。

最佳答案

您需要使用绝对路径而不是相对路径。我建议在重定向 URL 前面添加 /,如下所示:

new { @onchange="location='/' + this.value" })
<小时/>

此方法的唯一问题是,如果您部署到应用程序不在根目录的服务器(例如,http://myserver/myapp/Admin/Index) ,那么链接将不正确(即,它将指向http://myserver/Admin/Index)。因此,最好使用 Url.Action 帮助器:

@Html.DropDownList("Tables", new List<SelectListItem>
{
new SelectListItem { Text = "A", Value = Url.Action("Index", "Admin") },
new SelectListItem { Text = "B", Value = Url.Action("Index", "Admin") }
}, "WWW",new { @onchange="location=this.value" })

无论环境如何,这都将确保正确的 URL。

关于javascript - 使用 onchange 重定向到另一个 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18321591/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com