如何调用使用JQuery在ASP.NET MVC控制器动作 [英] How to Call Controller Actions using JQuery in ASP.NET MVC

查看:223
本文介绍了如何调用使用JQuery在ASP.NET MVC控制器动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读这一段时间,发现你可以调用通过使用控制器操作:

I've been reading on this for a while and found that you can call a controller action by using:

$.ajax("MyController/MyAction", function(data) {
    alert(data);
});

这是否意味着我要补充的MicrosoftMvcAjax.js或MicrosoftAjax.js随着jQuery的LIB?

Does this mean I should add the MicrosoftMvcAjax.js or the MicrosoftAjax.js along with the Jquery lib?

此外,什么应该第二个参数包含在$阿贾克斯()函数?

Also, what should the second parameter contain in the $.ajax() function?

最后,该网站在计算器或以外的任何其他环节,可能是有用的在asp.net mvc的瓦特/ AJAX和jQuery?

Lastly, any other link in stackoverflow or outside of the site that could be helpful in asp.net mvc w/ ajax and jquery?

感谢。

推荐答案

您可以从这里开始阅读 jQuery.ajax()

You can start reading from here jQuery.ajax()

其实控制器动作是可以通过URL访问的公共方法。因此,从Ajax调用一个动作的任何呼叫,无论是MicrosoftMvcAjax或jQuery的可以做。对我来说,jQuery是最简单的一种。它得到了很多在我给上面的链接的例子。典型的例子Ajax调用是这样的。

Actually Controller Action is a public method which can be accessed through Url. So any call of an Action from an Ajax call, either MicrosoftMvcAjax or jQuery can be made. For me, jQuery is the simplest one. It got a lots of examples in the link I gave above. The typical example for an ajax call is like this.

$.ajax({
    // edit to add steve's suggestion.
    //url: "/ControllerName/ActionName",
    url: '<%= Url.Action("ActionName", "ControllerName") %>',
    success: function(data) {
         // your data could be a View or Json or what ever you returned in your action method 
         // parse your data here
         alert(data);
    }
});

更多的例子可以在这里 中找到

More examples can be found in here

这篇关于如何调用使用JQuery在ASP.NET MVC控制器动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆