【发布时间】:2012-01-15 23:53:42
【问题描述】:
我正在寻找一种方法将给定类的所有现有复选框的值存储到某种列表或数组中,并通过 jquery 将结果发送到 asp.net mvc 控制器。复选框不必勾选,我需要全部。
更多细节:
我的复选框看起来像这样
<input type="checkbox" value="1" class="a-checkbox"
<input type="checkbox" value="2" class="a-checkbox"
<input type="checkbox" value="3" class="a-checkbox"
如果我的 MVC 控制器看起来像这样就好了
public JsonResult SaveList(List<String> values) { //... }
我知道我可以通过以下方式访问复选框
$('input.a-checkbox').each(
function () {
// create the list with the help of $(this).val()
}
);
// jquery post would happen here
但我不知道如何创建这样的数据结构。你能帮帮我吗?
谢谢
编辑:很好,谢谢。你能告诉我这有什么问题吗?我的控制器确实被调用了,但列表为空(在服务器端)
var list = [];
$('a-checkbox').each(
function () {
list.push($(this).val());
}
);
$.ajax({
type: "POST",
url: myUrl,
data: list,
success: function (data) {
alert(data.Result);
},
dataType: "json",
traditional: true
});
【问题讨论】:
-
如果值为 null,则表示您有模型绑定错误。这是我用来在调试中将绑定错误转储到控制台的扩展方法:pastebin.com/S0gM3vqg
标签: c# javascript jquery .net asp.net-mvc-3