【发布时间】:2014-08-15 10:30:08
【问题描述】:
好的,所以有很多关于这个的问题,但我似乎无法让它工作。应该很简单,但我摸不着头脑。
控制器:
[HttpPost]
public ActionResult Edit(BookingIndexViewModel vm) // Also tried BookingViewModel
{
return View();
}
容器视图模型:
public class BookingIndexViewModel
{
public int id { get; set; }
public List<BookingViewModel> bookings { get; set; }
public List<SelectListItem> allAttendances { get; set; }
}
预订视图模型(我真正感兴趣的虚拟机)
public class BookingViewModel
{
public int id { get; set; }
public int referralId { get; set; }
public int attendanceId { get; set; }
}
观点
@model Project.ViewModels.BookingIndexViewModel
@using Project.ViewModels
<fieldset>
<legend>Registered patients</legend>
@if (Model.bookings.Count < 1)
{
<div>None currently registered</div>
}
else
{
<table>
<tr>
<th>
<span class="display-label">@Html.LabelFor(model => model.bookings[0].forenames)</span>
</th>
<th>
<span class="display-label">@Html.LabelFor(model => model.bookings[0].surname)</span>
</th>
<th>
<span class="display-label">@Html.LabelFor(model => model.bookings[0].dob)</span>
</th>
<th>
<span class="display-label">@Html.LabelFor(model => model.bookings[0].attendance</span>
</th>
</tr>
@{
foreach (BookingViewModel b in Model.bookings)
{
using (Html.BeginForm("Edit", "Booking"))
{
@Html.HiddenFor(x => x.id)
<tr>
<td>
<span class="display-field">@b.forenames</span>
</td>
<td>
<span class="display-field">@b.surname</span>
</td>
<td>
<span class="display-field">@String.Format("{0:dd/MM/yyyy}", b.dob)</span>
</td>
<td>
@Html.DropDownListFor(model => b.attendanceStatusId, Model.allAttendances)
</td>
<td>
<input type="submit" value="Save" />
</td>
</tr>
}
}
}
</table>
}
</fieldset>
因此视图接受容器视图模型BookingIndexViewModel 并为每个持有的BookingViewModel 创建一个表单。提交后,我希望它使用修改后的BookingViewModel 传回容器视图模型,但它始终为空。我也尝试过期待以相同结果修改的单个BookingViewModel。 MVC 是否提交与给定视图的类型或从表单块中推断的类型相同类型的水合视图模型?
我从视图中检索到的唯一重要值是修改后的 attendanceId(应由下拉帮助程序填充)和预订 ID。
【问题讨论】:
标签: c# asp.net asp.net-mvc