使用JavaScript和MVC重定向到控制器
我正试图使用这行代码从JavaScript重定向到控制器使用JavaScript和MVC重定向到控制器,javascript,asp.net-mvc,model-view-controller,Javascript,Asp.net Mvc,Model View Controller,我正试图使用这行代码从JavaScript重定向到控制器 location.href = '/Dashboard/'; 它重定向到仪表板,但在我的仪表板视图中,在加载文档时调用此方法 $.post("Dashboard/UsersGet", {}, function (dataSet) { //do something with the dataset }); 然后我得到这个错误 POST http://localhost:1414/Dashboard/Dashboard/U
location.href = '/Dashboard/';
它重定向到仪表板,但在我的仪表板视图中,在加载文档时调用此方法
$.post("Dashboard/UsersGet", {}, function (dataSet) {
//do something with the dataset
});
然后我得到这个错误
POST http://localhost:1414/Dashboard/Dashboard/UsersGet 404 (Not Found)
我可以看到,仪表板被添加到url中两次。如何重定向到控制器而不发生这种情况?使用
Url
帮助程序:
@Url.Action("UsersGet", "Dashboard")
完整代码:
$.post('@Url.Action("UsersGet", "Dashboard")', {}, function (dataSet) {
//do something with the dataset
});
Asp.Net MVC中的路由与经典Asp.Net中的路由不同。尝试以下方法:
$.post("/Dashboard/UsersGet", {}, function (dataSet) {
//do something with the dataset
});
将/
添加到url
$.post("Dashboard/UsersGet", {}, function (dataSet) {
//do something with the dataset
});
应该是
$.post("/Dashboard/UsersGet", {}, function (dataSet) {
//do something with the dataset
});
如果没有“/”,您发布的url将附加到当前url。谢谢@gdoron,但请您详细说明我为什么要使用url。操作而不仅仅是添加正斜杠:)@johandklerk。你知道路线是怎么运作的吗?它们可以定制,Url助手知道如何处理它们。在asp.net中没有这样的东西。无论如何,在MVC中,您使用的是动作和控件,而不是URL。