MVC Jquery进入控制器动作

时间:2021-12-21 20:33:16

In Jquery, I need to tell the program to go to EmployeeController and Empl action. How do I tell it do this. I want to simple way to do this without using ajax.

在Jquery中,我需要告诉程序到EmployeeController和Empl操作。我该怎么说呢?我想用一种简单的方法来实现这一点,而不需要使用ajax。

4 个解决方案

#1


14  

window.location.href = "/{controller}/{action}" //in your case, /employee/empl

This works because of the routes specified in your Global.asax.cs file. I suggest you read up on how this works as it's one of the fundamentals of MVC....

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

#2


5  

Well, all good response so far, but using a magic string for building url just gives me the chills. I prefer to add an extension like this:

到目前为止,所有的响应都很好,但是使用一个用于构建url的神奇字符串只会让我胆战心惊。我更喜欢添加这样的扩展:

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);
}

and then in my js code use:

然后在我的js代码中:

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

It's more consistent, internally cares about routing, and gives me a centralized point where doing nasty things on Urls :)

它更加一致,内部更关心路由,并为我提供了一个集中点,在url上做一些令人讨厌的事情:)

Well, one more thing,

嗯,还有一件事,

window.location = "whatever";

is good and works, still

还好吗?

location.href = "whatever"; 

is preferreable.

preferreable。

HTH

HTH

#3


2  

Accepted answer doesn't work when the application is Published. I had to use

当应用程序发布时,接受的答案无效。我不得不使用

window.location.href='@Url.Action("ActionName","ControllerName")'

Hope it helps

希望它能帮助

#4


0  

Making assumptions about the URI structure & routing, but:

对URI结构和路由做出假设,但是:

window.location = '/employee/empl/';

窗口。位置= ' /员工/ empl / ';

#1


14  

window.location.href = "/{controller}/{action}" //in your case, /employee/empl

This works because of the routes specified in your Global.asax.cs file. I suggest you read up on how this works as it's one of the fundamentals of MVC....

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

#2


5  

Well, all good response so far, but using a magic string for building url just gives me the chills. I prefer to add an extension like this:

到目前为止,所有的响应都很好,但是使用一个用于构建url的神奇字符串只会让我胆战心惊。我更喜欢添加这样的扩展:

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);
}

and then in my js code use:

然后在我的js代码中:

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

It's more consistent, internally cares about routing, and gives me a centralized point where doing nasty things on Urls :)

它更加一致,内部更关心路由,并为我提供了一个集中点,在url上做一些令人讨厌的事情:)

Well, one more thing,

嗯,还有一件事,

window.location = "whatever";

is good and works, still

还好吗?

location.href = "whatever"; 

is preferreable.

preferreable。

HTH

HTH

#3


2  

Accepted answer doesn't work when the application is Published. I had to use

当应用程序发布时,接受的答案无效。我不得不使用

window.location.href='@Url.Action("ActionName","ControllerName")'

Hope it helps

希望它能帮助

#4


0  

Making assumptions about the URI structure & routing, but:

对URI结构和路由做出假设,但是:

window.location = '/employee/empl/';

窗口。位置= ' /员工/ empl / ';