将特定表数据从视图返回到控制器? asp.net MVC

时间:2017-07-25 17:56:21

标签: asp.net-mvc

所以我有一个视图,它正在显示数据库中的数据表,其中包含select / selectall复选框。我想做的是能够获得我的CustomerNumbers列表以及它们是否已被检查并将其返回给我的控制器以进行进一步操作。

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;
}
});

1 个答案:

答案 0 :(得分:0)

不确定这是否是处理它的最佳方式,但我只是将我想要的字段添加到HiddenFor帮助程序中,然后将它们返回到我的相应控制器。谢谢大家的帮助。

 @Html.HiddenFor(m => m.additionalCustomerInfoListView[i].CustomerNumber)