如何通过JQuery的阿贾克斯传递数组以及如何concieve它的服务器端? [英] How to pass an array through in JQuery Ajax and how to concieve it in server side?
问题描述
我有一个问题在这里我必须通过ID列表,以服务器端删除一些用户,但我的要求是使用JQuery Ajax来做到这一点。但我无法得到我的服务器端的参数有谁能够给这个帮助理清PLS?
I have a problem where i have to pass a list of ids to serverside to delete some users, but my requirement is to do it using JQuery Ajax. But I was not able to get the parameters in my server side can anybody give a help on this to sort out pls?
我做了什么至今如下所示
What i have done till now is shown below
var idList = new Array();
$(document).ready(function () {
$('input:checkbox').click(function () {
//set our checkedcount variable to 0
var checkedCount = 0;
//loop through and count the number of "checked" boxes
$('.acceptUsers:checked').each(function () {
//if a checked box was found, increase checkedCount by 1
idList.push($(this).val());
checkedCount++;
});
});
});
$('#btnDelete').click(function () {
url = 'Teacher/UpdateUserStatus/';
var ids = idList.toString();
$.ajax({
type: "POST",
url: url,
data: { 'userIds': ids },
contentType: "application/json; charset=utf-8",
dataType: "html",
success: function (data) {
alert('yeah');
}
}
});
});
我在我的服务器端做的是
What i have done in my server side is
[HttpPost]
public JsonResult UpdateUserStatus(object userIds)
{
List<int> usersToDelete = new JavaScriptSerializer().ConvertToType<List<int>>(userIds);
this.userService.DeleteUsers(usersToDelete);
return Json(true, JsonRequestBehavior.AllowGet);
}
任何人都可以知道为什么我的服务器端方法不打电话来?
Can anybody know why my server side method is not calling?
推荐答案
第一件事我注意到的是,你有一个大括号的code里面多余的。请尝试以下之一:
First thing that I have noticed is that you have one curly brace extra inside your code. Try the following one :
$('#btnDelete').click(function () {
url = 'Teacher/UpdateUserStatus/';
var ids = idList.toString();
$.ajax({
type: "POST",
url: url,
data: { 'userIds': ids },
contentType: "application/json; charset=utf-8",
dataType: "html",
success: function (data) {
alert('yeah');
}
});
});
如果这不工作,启动调试会话,把一个破发点上的操作方法,看看你打的服务器上。
If this is not working, start a debug session and put a break-point on the action method and see if you are hitting the server or not.
另外看看下面的文章jQuery的阿贾克斯的例子在ASP.NET MVC 3调用:
Also have a look at the following article for an example of JQuery Ajax calls on ASP.NET MVC 3 :
<一个href=\"http://www.tugberkugurlu.com/archive/working-with-jquery-ajax-api-on-asp-net-mvc-3-0-power-of-json-jquery-and-asp-net-mvc-partial-views\" rel=\"nofollow\">http://www.tugberkugurlu.com/archive/working-with-jquery-ajax-api-on-asp-net-mvc-3-0-power-of-json-jquery-and-asp-net-mvc-partial-views
这篇关于如何通过JQuery的阿贾克斯传递数组以及如何concieve它的服务器端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!