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");
}
はこちらもチェックボックスのための私のjavascriptが
[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;
}
});
は思え' 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)。 'Select All'チェックボックスを処理するにはjavascriptが必要です –
@StephenMuecke 'foreach'を保ち、' var i = Model.CustomerInfoListView.IndexOf(item); 'を使用して各繰り返しのインデックス位置を取得できますか? – zgood