MVC Jquery转到控制器动作

Nat*_*Pet 10 asp.net-mvc jquery

在Jquery中,我需要告诉程序转到EmployeeController和Empl动作.我怎么告诉它这样做.我想以简单的方式在不使用ajax的情况下执行此操作.

Ada*_*son 17

window.location.href = "/{controller}/{action}" //in your case, /employee/empl
Run Code Online (Sandbox Code Playgroud)

这是因为Global.asax.cs文件中指定的路由.我建议你阅读它是如何工作的,因为它是MVC的基础之一....


Big*_*ike 5

好吧,到目前为止所有良好的反应,但使用魔法字符串构建网址只是让我发冷.我更喜欢添加这样的扩展名:

public static string GetUrl(this HtmlHelper, helper, string Action, string Controller, object RouteValues)
{
    UrlHelper Url = new UrlHelper(HttpContext.Current.Request.RequestContext);
    return Url.Action(Action, Controller, RouteValues);
}
Run Code Online (Sandbox Code Playgroud)

然后在我的js代码中使用:

location.href = '@Html.GetUrl("Action", "Controller", new { foo=Model.Foo })';
Run Code Online (Sandbox Code Playgroud)

它更一致,内部关心路由,并给我一个集中点,在Urls上做令人讨厌的事情:)

好吧,还有一件事,

window.location = "whatever";
Run Code Online (Sandbox Code Playgroud)

很好,也很有效

location.href = "whatever"; 
Run Code Online (Sandbox Code Playgroud)

是可取的.

HTH