我有一個IList對象,使用ViewModel傳遞給我的視圖。在MVC視圖中使用複選框,並將選中的狀態傳遞迴控制器進行處理
[HttpGet]
public ActionResult Reminder()
{
using(var db = new SalonContext())
{
var tomorrow = DateTime.Today.AddDays(1);
var homeSalon = GetHomeSalonId();
var myAppointments = db.Appointments.Include(c => c.Customer).Include(c => c.Treatment).Include(c => c.Stylist).Include(c => c.Salon).Where(c => c.SalonId == homeSalon).ToList();
var tomorrowsAppointments = myAppointments.Where(a => a.Start.Date == tomorrow).Select(rem => new FutureAppointment
{
AppointmentId = rem.AppointmentId,
Name = rem.Customer.Title + " " +rem.Customer.FirstName + " " + rem.Customer.Surname,
AppointmentTime = rem.Start,
Salon = rem.Salon.Name,
Stylist = rem.Stylist.FirstName,
Treatment = rem.Treatment.Name,
Email = rem.Customer.EMail,
SendReminder = true
}).ToList();
return View(tomorrowsAppointments);
}
}
視圖呈現列表使用此代碼的複選框:
@model IList<MySalonOrganiser.Models.FutureAppointment>
@{
ViewBag.Title = "Email Reminders";
}
<h2>@ViewBag.Title</h2>
@using(Html.BeginForm("Reminder", "Appointment", FormMethod.Post))
{
@Html.AntiForgeryToken()
<table class="table">
<tr>
<th>
Send Reminder
</th>
<th>
Name
</th>
<th>
Appointment Time
</th>
<th>
Salon
</th>
<th>
Treatment
</th>
<th>
Stylist
</th>
<th>
EMail
</th>
<th></th>
</tr>
@for (var i =0; i < Model.Count; i++)
{
<tr>
<td>
@Html.CheckBoxFor(x => x[i].SendReminder)
</td>
<td>
@Html.DisplayFor(x => x[i].Name)
</td>
<td>
@Html.DisplayFor(x => x[i].AppointmentTime)
</td>
<td>
@Html.DisplayFor(x => x[i].Salon)
</td>
<td>
@Html.DisplayFor(x => x[i].Treatment)
</td>
<td>
@Html.DisplayFor(x => x[i].Stylist)
</td>
<td>
@Html.DisplayFor(x => x[i].Email)
</td>
</tr>
}
</table>
<div>
<input type="Submit" value="Send Reminders" />
</div>
}
當我發表的看法回到控制器,我看到在返回的模型類型FutureAppointment名單的項目,但除複選框狀態以外的所有內容都爲空。
任何人都可以幫我解釋爲什麼和如何解決這個問題,所以我看到所有的模型數據?
Jason。