2017-07-25 9 views
1

私は現在、select/selectallチェックボックスを使って、データベースからのデータの表を表示しています。私がしたいのは、CustomerNumbersのリストを取得し、チェックされているかどうかを確認して、それをコントローラに返して、それ以上の操作を行うことです。以下は特定のテーブルデータをビューからコントローラに戻していますか? asp.net MVC

enter image description here

は、私の見解では、現在書かれている私の方法です。私はこれを動作させるために数多くの方法を試みましたが、私は困惑しています。

@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; 
} 
}); 
+2

は思え' Selected'、その後、どこ '' あなたが使用できるある '@ Html.EditorFor(M => m.CustomerInfoListView [I] .Selected) '(ここで' i'はその項目のインデックス位置です)。コントローラに投稿すると、そのデータにアクセスできます。 – zgood

+0

コレクションのフォームコントロールを生成するために '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が必要です –

+0

@StephenMuecke 'foreach'を保ち、' var i = Model.CustomerInfoListView.IndexOf(item); 'を使用して各繰り返しのインデックス位置を取得できますか? – zgood

答えて

0

それはそれを処理するための最良の方法であるかどうかわからないですが、私は単純にフィールドを追加しました私はHiddenForヘルパーに欲しいと思って、彼らは私の適切なコントローラに戻ってきました。助けをいただき、ありがとうございました。あなたがあなたの名前の `CustomerInfoListView`ビューモデルに新しいプロパティを必要とするよう

@Html.HiddenFor(m => m.additionalCustomerInfoListView[i].CustomerNumber) 
関連する問題