使用 jQuery 调用控制器动作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1334447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using jQuery To Call A Controller Action
提问by RailRhoad
I have a nice page that does everything I need. However one of the elements (a partial page) takes a few seconds longer then I'd like to load. So what I'd like to do is to show the page without this partial first but show a "loading" gif in its place. Then in my jquery...
我有一个很好的页面,可以满足我的所有需求。但是,其中一个元素(部分页面)比我想加载的时间长几秒钟。所以我想做的是首先显示没有这个部分的页面,但在它的位置显示一个“加载”gif。然后在我的 jquery ...
$(document).ready(function() {
// Call controller/action (i.e. Client/GetStuff)
});
I'd like to call my controller action that returns a PartialView and updates my div contents. It basically calls the partial load asynchronously on load. I could do this just fine with an ActionLink until it get's to the point where I'd like to do it onload. If I use jQuery for the onloand type call, can I even return a PartialView?
我想调用返回 PartialView 并更新我的 div 内容的控制器操作。它基本上在加载时异步调用部分加载。我可以用 ActionLink 很好地做到这一点,直到它达到我想要在加载时执行的程度。如果我使用 jQuery 进行 onloand 类型调用,我什至可以返回 PartialView 吗?
回答by karim79
Simply use $.load:
只需使用$.load:
$(document).ready(function() {
$('#myDiv').html('<img src="loading.gif"/>')
.load('Client/GetStuff');
});
That will replace the contents of div id="myDiv" with your loading indicator, and then inject the output of Client/GetStuff into it.
这将用您的加载指示器替换 div id="myDiv" 的内容,然后将 Client/GetStuff 的输出注入其中。
回答by Dan Esparza
I believe you can. I've used jQuery to get JSON from an ASP.NET MVC controller before, and it's one of the most pleasant ways I've found to get data to the client.
我相信你可以。我以前曾使用 jQuery 从 ASP.NET MVC 控制器获取 JSON,这是我发现将数据发送到客户端的最愉快的方法之一。
I'm sure getting a partial view might be as simple as using the jQuery 'load', 'get' or 'post' methods:
我确信获得局部视图可能就像使用 jQuery 'load'、'get' 或 'post' 方法一样简单:
Using Load:
使用负载:
$("#feeds").load("test.aspx");
Using Get:
使用获取:
$.get("test.aspx", function(data){
alert("Data Loaded: " + data);
});
Using Post:
使用邮政:
$.post("test.aspx", function(data){
alert("Data Loaded: " + data);
});
回答by Fenton
$.ajax("MyController/MyAction", function(data) {
alert(data);
});
This is a really basic example; you just call the controller using AJAX and then you can pop the data into the DOM or do something else with it.
这是一个非常基本的例子;您只需使用 AJAX 调用控制器,然后您就可以将数据弹出到 DOM 中或用它做其他事情。
回答by Abhishek Gahlout
We can call Controller method using Javascript / Jquery very easily as follows:
我们可以很容易地使用 Javascript / Jquery 调用 Controller 方法,如下所示:
Suppose following is the Controller method to be called returning an array of some class objects. Let the class is 'A'
假设以下是要调用的 Controller 方法,返回一些类对象的数组。让班级是'A'
public JsonResult SubMenu_Click(string param1, string param2)
{
A[] arr = null;
try
{
Processing...
Get Result and fill arr.
}
catch { }
return Json(arr , JsonRequestBehavior.AllowGet);
}
Following is the complex type (class)
以下是复杂类型(类)
public class A
{
public string property1 {get ; set ;}
public string property2 {get ; set ;}
}
Now it was turn to call above controller method by JQUERY. Following is the Jquery function to call the controller method.
现在轮到 JQUERY 调用上面的控制器方法了。下面是调用控制器方法的Jquery函数。
function callControllerMethod(value1 , value2) {
var strMethodUrl = '@Url.Action("SubMenu_Click", "Home")?param1=value1 ¶m2=value2'
$.getJSON(strMethodUrl, receieveResponse);
}
function receieveResponse(response) {
if (response != null) {
for (var i = 0; i < response.length; i++) {
alert(response[i].property1);
}
}
}
In the above Jquery function 'callControllerMethod' we develop controller method url and put that in a variable named 'strMehodUrl' and call getJSON method of Jquery API.
在上面的 Jquery 函数“callControllerMethod”中,我们开发了控制器方法 url 并将其放入名为“strMehodUrl”的变量中,然后调用 Jquery API 的 getJSON 方法。
receieveResponse is the callback function receiving the response or return value of the controllers method.
receieveResponse 是接收控制器方法的响应或返回值的回调函数。
Here we made use of JSON , since we can't make use of the C# class object
这里我们使用了 JSON ,因为我们不能使用 C# 类对象
directly into the javascript function , so we converted the result (arr) in controller method into JSON object as follows:
直接进入javascript函数,所以我们将控制器方法中的结果(arr)转换为JSON对象,如下所示:
Json(arr , JsonRequestBehavior.AllowGet);
Json(arr , JsonRequestBehavior.AllowGet);
and returned that Json object.
并返回该 Json 对象。
Now in callback function of the Javascript / JQuery we can make use of this resultant JSON object and work accordingly to show response data on UI.
现在在 Javascript / JQuery 的回调函数中,我们可以利用这个结果 JSON 对象并相应地在 UI 上显示响应数据。
For more detaill click here
更多详情请点击这里