MVC表单无法发布对象列表
@model IEnumerable<PlanCompareViewModel>@using (Html.BeginForm("ComparePlans", "Plans", FormMethod.Post, new { id = "compareForm" })){<div> @foreach (var planVM in Model) { @Html.Partial("_partialView", planVM) }</div>}
@model PlanCompareViewModel<div> @Html.HiddenFor(p => p.PlanID) @Html.HiddenFor(p => p.CurrentPlan) @Html.CheckBoxFor(p => p.ShouldCompare) <input type="submit" value="Compare"/></div>
public class PlansCompareViewModel{ public int PlanID { get; set; } public Plan CurrentPlan { get; set; } public bool ShouldCompare { get; set; } public PlansCompareViewModel(Plan plan) { ShouldCompare = false; PlanID = plan.PlanId; CurrentPlan = plan; } public PlansCompareViewModel() { // TODO: Complete member initialization } public static IEnumerable<PlansCompareViewModel> CreatePlansVM(IEnumerable<Plan> plans) { return plans.Select(p => new PlansCompareViewModel(p)).AsEnumerable(); }}
public class PlansController : MyBaseController{ [HttpPost] public ActionResult ComparePlans(IEnumerable<PlanCompareViewModel> model) { //the model passed into here is NULL }}