【发布时间】:2014-08-24 08:35:47
【问题描述】:
我有一个视图,其中包含许多复选框。我希望能够将复选框的值传递给控制器,然后输出已勾选的 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>
控制器:
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; }
}
}
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; }
}
【问题讨论】:
-
您应该在模型中使用与您的项目类似的复选框定义
-
你这是什么意思?我是 MVC 新手,所以我不完全理解
-
我已编辑问题以包含模型
-
我会看看这个问题,看起来很相似:stackoverflow.com/questions/220020/…
标签: asp.net-mvc checkbox