1
所以我有一個視圖,它正在顯示一個數據庫中的數據表,並帶有select/selectall複選框。我想要做的是能夠獲得我的CustomerNumbers列表以及他們是否已被檢查並將其返回給我的控制器以進一步採取行動。將視圖中的特定表數據返回給控制器? asp.net MVC
下面是我的看法是我當前如何寫的。我嘗試了很多方法來使這個工作,我很難過。
@model MassInactiveInspections.Models.CustomerInfoList
<div class="container">
<h2>@ViewBag.Title</h2>
<div class="bs-callout bs-callout-info" style="margin-bottom: 0px;">
</div>
<div class="col-md-8">
@using (Html.BeginForm("SelectedCustomers", "Home", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
<hr />
@Html.ValidationSummary(true)
<div class="form-group">
<table class="table table-bordered">
<tr>
<th class="active">@Html.DisplayName("Select All?")<input type="checkbox" class="select-all checkbox" name="select-all" /></th>
<th>@Html.DisplayNameFor(m => m.CustomerInfoListView.FirstOrDefault().BusinessName)</th>
<th>@Html.DisplayNameFor(m => m.CustomerInfoListView.FirstOrDefault().CustomerName)</th>
<th>@Html.DisplayNameFor(m => m.CustomerInfoListView.FirstOrDefault().CustomerNumber)</th>
<th>@Html.DisplayNameFor(m => m.CustomerInfoListView.FirstOrDefault().InspectionCycleDescription)</th>
<th>@Html.DisplayNameFor(m => m.CustomerInfoListView.FirstOrDefault().LastInspectionDate)</th>
<th>@Html.DisplayNameFor(m => m.CustomerInfoListView.FirstOrDefault().RouteCode)</th>
<th>@Html.DisplayNameFor(m => m.CustomerInfoListView.FirstOrDefault().RouteID)</th>
<th>@Html.DisplayNameFor(m => m.CustomerInfoListView.FirstOrDefault().SiteNumber)</th>
<th>@Html.DisplayNameFor(m => m.CustomerInfoListView.FirstOrDefault().SystemCode)</th>
</tr>
@foreach (var item in Model.CustomerInfoListView)
{
<tr>
<td class="active"> <input id="Selected" input type="checkbox" class="select-item checkbox" name="select-item" value="1000" /> </td>
<td>@Html.DisplayFor(modelItem => item.BusinessName)</td>
<td>@Html.DisplayFor(modelItem => item.CustomerName)</td>
<td>
@Html.DisplayFor(modelItem => item.CustomerNumber)
@Html.HiddenFor(m => m.CustomerInfoListView.FirstOrDefault().CustomerNumber)
</td>
<td>@Html.DisplayFor(modelItem => item.InspectionCycleDescription)</td>
<td>@Html.DisplayFor(modelItem => item.LastInspectionDate)</td>
<td>@Html.DisplayFor(modelItem => item.RouteCode)</td>
<td>@Html.DisplayFor(modelItem => item.RouteID)</td>
<td>@Html.DisplayFor(modelItem => item.SiteNumber)</td>
<td>@Html.DisplayFor(modelItem => item.SystemCode)</td>
</tr>
}
</table>
</div>
<div class="form-group">
<div style="margin-top: 50px">
<input type="submit" id="btnLost" class="btn btn-primary" value="Lost"/>
<input type="submit" class="btn btn-primary" name="OOBButton" value="OOB" />
<input type="submit" class="btn btn-primary" name="RefusedButton" value="Refused" />
</div>
</div>
}
</div>
</div>
最後我控制器
[HttpPost]
public ActionResult SelectedCustomers(CustomerInfoList
customerNumberList)
{
return View("ViewCustomerInfo");
}
也在這裏是我的複選框
[HttpPost]
public ActionResult SelectedCustomers(CustomerInfoList customerNumberList)
{
return View("ViewCustomerInfo");
}
//column checkbox select all or cancel
$("input.select-all").click(function() {
var checked = this.checked;
$("input.select-item").each(function (index, item) {
item.checked = checked;
});
});
//check selected items
$("input.select-item").click(function() {
var checked = this.checked;
console.log(checked);
checkSelected();
});
//check is all selected
function checkSelected() {
var all = $("input.select-all")[0];
var total = $("input.select-item").length;
var len = $("input.select-item:checked:checked").length;
console.log("total:" + total);
console.log("len:" + len);
all.checked = len === total;
}
});
好像你需要命名你的'CustomerInfoListView'視圖模型一個新的屬性'Selected',然後在您的''是你可以用'@ Html.EditorFor(M => m.CustomerInfoListView [I] .Selected)'在你的循環中(其中'i'是該項目的索引位置)。然後,當您發佈到控制器時,您將可以訪問該數據。 – zgood
你不能使用'foreach'循環爲集合生成表單控件 - 參考[this answer](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943 )。並處理您的複選框併發回選擇 - 請參閱[這個答案](https://stackoverflow.com/questions/29542107/pass-list-of-checkboxes-into-view-and-pull-out-ienumerable/29554416 #29554416)。你會需要一些JavaScript來處理你的'全選'複選框 –
@StephenMuecke他可以保留'foreach'並使用'var i = Model.CustomerInfoListView.IndexOf(item);'來獲得每次迭代的索引位置嗎? – zgood