Javascript MVC4:单击按钮时将复选框值作为HtmlAttributes发送
我的MVC4网页上有一个复选框列表,生成方式如下:Javascript MVC4:单击按钮时将复选框值作为HtmlAttributes发送,javascript,jquery,html,asp.net-mvc-4,Javascript,Jquery,Html,Asp.net Mvc 4,我的MVC4网页上有一个复选框列表,生成方式如下: <div style="float: left; padding-left: 5px; padding-right: 5px"> <br /> <h3>@Html.Label("Type de service")</h3> @foreach (var serviceType in Model.ServiceTypeList) { <label
<div style="float: left; padding-left: 5px; padding-right: 5px">
<br />
<h3>@Html.Label("Type de service")</h3>
@foreach (var serviceType in Model.ServiceTypeList)
{
<label>@Html.CheckBox(serviceType.ServiceTypeId.ToString(CultureInfo.InvariantCulture)) @serviceType.Description</label><br />
}
<br />
<h3>@Html.Label("Type d'application")</h3>
@foreach (var appType in Model.ApplicationTypeList)
{
<label>@Html.CheckBox(appType.ApplicationId.ToString()) @appType.ApplicationName</label><br />
}
<br />
</div>
我现在要做的是,当单击按钮时,将两个复选框列表中每一个的字典发送回服务器,其中包含一个键/值对,其ID为复选框,布尔值为:
<div style="float: left; padding-left: 15px; padding-right: 5px;">
@using (Html.BeginForm("Filter", "Customer", FormMethod.Post, new Dictionary<string, bool>
{}))
{
<input id="btnFilter" type="submit" value="Filter" />
}
</div>
动态获取两个列表的值并将其发送到服务器的最佳方法是什么?类似的方法应该可以工作,它将为每个列表创建2个数组,每个列表的结构如下所示 [id boolean,id boolean,id boolean,id boolean,]:
<script>
$(function() {
$("#button").click(function() {
var listOneArray = [];
var listTwoArray = [];
$("#listOne input[type='checkbox']").each(function() {
var a1 = $(this).attr("id");
var b1 = $(this).prop("checked");
listOneArray.push(a1 + "-" + b1.toString());
});
$("#listtwo input[type='checkbox']").each(function () {
var a2 = $(this).attr("id");
var b2 = $(this).prop("checked");
listTwoArray.push(a2 + "-" + b2.toString());
});
$.post("Yourcontroller/YourAction", { list1: listOneArray, list2: listTwoArray }, function (data) {
//do whatever with the response
});
});
});
</script>
您想将此作为ajax调用发布吗?不清楚为什么要这样生成视图,因此无法绑定回模型