gpt4 book ai didi

asp.net-mvc - 将复选框的值从 View 传递到 Controller

转载 作者:行者123 更新时间:2023-12-02 17:30:46 26 4
gpt4 key购买 nike

我有一个 View ,其中有许多复选框。我希望能够将复选框的值传递给 Controller ​​,然后输出已勾选的 OfficeName 列表。我不确定如何将多个复选框的值传递回 Controller ,或者如何根据已勾选的框输出 OfficeName

查看:

<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>

Controller :

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

型号:

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

投注办公室类:

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

最佳答案

试试这个:

<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>

在您的操作中,您可以在 bettingOfficeIDs 变量中获取所选办公室 ID:

 public ActionResult YourActionName(int[] bettingOfficeIDs)

关于asp.net-mvc - 将复选框的值从 View 传递到 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24549988/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com