jquery ajax asp.net mvc
我想将数据发布到服务器。在我选择的表单中,选中复选框并从中检索值。我使用了以下代码: $(".DownloadSelected").click(function () { var values = []; var chks = $(':checkbox[name="ids"]:checked'); $(chks).each(function (i) { values[i] = $(this).val(); }); $.post("/Documents/DownloadSelected/", { ids: values }); }); $(“.DownloadSelected”)。单击(函数(){ var值=[]; var chks=$(':checkbox[name=“ids”]:checked'); $(chks)。每个(功能(i){ 值[i]=$(this.val(); }); $.post(“/Documents/DownloadSelected/”,{id:values}); }); 在控制器中,我有: [HttpPost] public ActionResult DownloadSelected(int[] ids) { } [HttpPost] public ActionResult DownloadSelected(int[]ID) { } 问题是,在控制器中,我检索到int[]ids数组中的空值。jquery ajax asp.net mvc,jquery,asp.net-mvc,ajax,Jquery,Asp.net Mvc,Ajax,我想将数据发布到服务器。在我选择的表单中,选中复选框并从中检索值。我使用了以下代码: $(".DownloadSelected").click(function () { var values = []; var chks = $(':checkbox[name="ids"]:checked'); $(chks).each(function (i) { values[i] = $(this)
有人能帮我吗?javascript代码中的值不是作为int数组传递的,请尝试将每个值解析为int
parseInt($(this).val())代码>试试这个
$(".DownloadSelected").click(function () {
var chks = $(':checkbox[name="ids"]:checked');
var values= $(chks).each(function (i) {
return i.val();
});
values.join(',');
$.post("/Documents/DownloadSelected/", { ids: values });
});
和在控制器中
[HttpPost]
public ActionResult DownloadSelected(List<int> ids)
{
}
[HttpPost]
public ActionResult DownloadSelected(列表ID)
{
}
您应该定义复杂类型并定义属性int[]
public class PostModel
{
public int[] Ids { get; set; }
}
和在控制器中
[HttpPost]
public ActionResult DownloadSelected(PostModel postModel)
{
}
只需将您的复选框输入放入如下表单中
<form id="frm">
<input type="checkbox" name="ids" value="1">
<input type="checkbox" name="ids" value="2">
<input type="checkbox" name="ids" value="3">
</form>
谢谢,伙计们,我是用通常的$.ajax()函数而不是$.post()来解决这个问题的。问题是,我必须在$.ajax()函数的ajax选项中使用{traditional:true}。
var form=$("#frm");
$.ajax({
type: "POST",
url: "your action url here",
data: form.serialize(), // serializes the form's elements.
success: function(data)
{
alert(data);
}
});