2017-07-25 99 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"); 
} 

也在这里是我的复选框

[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

好像你需要命名你的'CustomerInfoListView'视图模型一个新的属性'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)。你会需要一些JavaScript来处理你的'全选'复选框 –

+0

@StephenMuecke他可以保留'foreach'并使用'var i = Model.CustomerInfoListView.IndexOf(item);'来获得每次迭代的索引位置吗? – zgood

回答

0

不知道这是处理的最佳方式,但我只是添加的字段的javascript我想成为一名HiddenFor帮手,并将他们退回给我的相应控制器。感谢大家的帮助。

@Html.HiddenFor(m => m.additionalCustomerInfoListView[i].CustomerNumber) 
相关问题