如何接受值通过AJAX传递 [英] how to accept value passed through ajax
问题描述
我写了一个code传递的这是由用户通过点击复选框选中这些记录的ID
I wrote a code to pass id's of those records which are selected by user by clicking checkbox
在BTTN的点击提交下列code被称为
on click of bttn submit following code is called
$("#button").click(function () {
var selected = $(".a:checked").map(function() {
return this.id;
}).get();
var urlDistricts = '@Url.Action("selectedId")';
$.ajax({
type: "POST",
url: urlDistricts,
data: { listofid:selected },
success: function () {
}
});
});
和捕获这个我在我的控制器写了以下
and to catch this i wrote following in my controller
[HttpPost]
public ActionResult selectedId(List<int> listofid)
{
return View();
}
但在 listofid 是卡明空
推荐答案
将传统:真正的
参数:
$.ajax({
type: "POST",
url: urlDistricts,
traditional: true,
data: { listofid: selected },
success: function (result) {
}
});
也似乎有什么问题你code。您检索选择复选框的ID。因此我可以假设您的标记看起来像这样:
Also there seems to be something wrong with your code. You are retrieving the ids of the selected checkboxes. So I can assume that your markup looks like this:
<input class="a" type="checkbox" id="1" name="id1" />
<input class="a" type="checkbox" id="2" name="id2" />
<input class="a" type="checkbox" id="3" name="id3" />
...
我假设你的IDS都是数字,因为你正试图将它们绑定到一个列表与LT; INT&GT;
在你的控制器动作。只不过这是无效的HTML。 IDS不能以数字开头。因此,一种可能是使用HTML5 数据 - *
属性上的复选框来存储这些额外的元数据:
I assume that your ids are numeric because you are attempting to bind them to a List<int>
in your controller action. Except that this is invalid HTML. Ids cannot start with a number. So one possibility is to use HTML5 data-*
attribute on your checkboxes to store this additional metadata:
<input class="a" type="checkbox" data-id="1" name="id1" />
<input class="a" type="checkbox" data-id="2" name="id2" />
<input class="a" type="checkbox" data-id="3" name="id3" />
...
和则:
var selected = $(".a:checked").map(function() {
return $(this).data('id');
}).get();
这篇关于如何接受值通过AJAX传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!