将复选框的值从View传递给Controller

Kev*_*vin 4 checkbox asp.net-mvc

我有一个包含许多复选框的视图.我希望能够将复选框的值传递给控制器​​,然后输出已经勾选的OfficeNames列表.我不知道如何将多个复选框的值传递回控制器,或者如何根据已勾选的框输出OfficeNames

视图:

<p>
@using (Html.BeginForm())
{
<p>
    Start Date: @Html.TextBox("StartDate") <br />
    <br />
    End Date: @Html.TextBox("EndDate") <br />
    <br />
    <input type="submit" value="Filter" />
</p>
}

<p>
@foreach (var item in Model.BettingOffices)
{
    <label>@Html.DisplayFor(modelItem => item.OfficeName)</label>
    <input type="checkbox" name="selectedShops" value="@item.OfficeName">
}

</p>
Run Code Online (Sandbox Code Playgroud)

控制器:

public class DailyReportController : Controller
{
    private RiskEntities _db = new RiskEntities();

    // GET: /DailyReport/
    public ActionResult Index(DateTime? startDate, DateTime? endDate)
    {

        if (startDate == null || endDate == null)
        {
            var dailyReportModelBlank = new DailyReportModel();
            dailyReportModelBlank.BettingOffices = (from bo in _db.BettingOffices orderby bo.OfficeName select bo ).ToList();
            //dailyReportModelBlank.DailyReports.Add(new DailyReport());
            return View(dailyReportModelBlank);
        }

        var endDateToUse = (DateTime) endDate;
        endDateToUse = endDateToUse.AddDays(+1);


        var dailyReportModel = new DailyReportModel
        {
            DailyReports = (from dr in _db.DailyReports
                where dr.DailyReportDate >= startDate
                      && dr.DailyReportDate <= endDateToUse
                select dr).ToList(),
            BettingOffices = (from bo in _db.BettingOffices select bo).ToList()
        };


        return View(dailyReportModel);
    }
Run Code Online (Sandbox Code Playgroud)

模型:

public class DailyReportModel
{
    private List<DailyReport> _dailyReports = new List<DailyReport>();
    private List<BettingOffice> _bettingOffices = new List<BettingOffice>();

    public List<DailyReport> DailyReports
    {
        get { return _dailyReports; }
        set { _dailyReports = value; }
    }

    public List<BettingOffice> BettingOffices
    {
        get { return _bettingOffices; }
        set { _bettingOffices = value; }
    }
}
Run Code Online (Sandbox Code Playgroud)

BettingOffice类:

public partial class BettingOffice
{
    public int BettingOfficeID { get; set; }
    public string OfficeName { get; set; }
    public string OfficeCode { get; set; }
    public string IpAddress { get; set; }
    public Nullable<bool> SupportOnly { get; set; }
    public Nullable<int> SisSrNumer { get; set; }
    public Nullable<bool> Local { get; set; }
    public string Server { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

小智 8

试试这个 :

<p>
    @using (Html.BeginForm())
    {
        <p>
            Start Date: @Html.TextBox("StartDate")
            <br />
            <br />
            End Date: @Html.TextBox("EndDate")
            <br />
            <br />
            <input type="submit" value="Filter" />
        </p>
    }
</p>
<p>
    @foreach (var item in Model.BettingOffices)
    {
        <label>@Html.DisplayFor(modelItem => item.OfficeName)</label>
        <input type="checkbox" name="bettingOfficeIDs" value="@item.BettingOfficeID">
    }
</p>
Run Code Online (Sandbox Code Playgroud)

在您的Action中,您可以在bettingOfficeIDs变量中获取所选的办公室id:

 public ActionResult YourActionName(int[] bettingOfficeIDs)
Run Code Online (Sandbox Code Playgroud)