Javascript window.location 在 MVC3 中不起作用

本文关键字:不起作用 MVC3 window location Javascript | 更新日期: 2023-09-27 18:35:10

我有一个页面,它通过一个名为EntityIndexActionResult呈现,该将int id作为参数并加载该实体。

在此视图上,用户可以从下拉列表中选择其他相关实体,并且视图应通过将下拉列表中的所选 ID 发送到具有新ID的同一操作EntityIndex来重新加载。

我正在使用下拉列表中的jQuery更改事件来导航和重新加载页面:

$("#RelatedEntity").change(function () {
    window.location = '@Url.Action("EntityIndex", new {id = ""})' + '/' + $(this).val();
});

这是行动

public ActionResult EntityIndex(int id) {
    ... gets entity by id here ...
    return View(model);
}

该操作在命中时工作正常,但上面的 jQuery 行失败并显示错误:

http://localhost:1798/Entity/EntityIndex/@Url.Action("EntityIndex", new {id = ""})/539

出于某种原因,触发@Url.Action window.location将操作视为字符串,而不是要导航到的操作...阻止其正常运行的Url.Action有什么问题?

Javascript window.location 在 MVC3 中不起作用

你的 JQuery 总是有点偏离。使用默认路由并指定任何 ID 将生成:

/

控制器/动作/

所以你需要做的就是把你的价值放在最后。试试这个:

$("#RelatedEntity").change(function () {
    window.location = '@Url.Action("EntityIndex")' + $(this).val();
});

应该给你(假设值为 23):

/

控制器/动作/23