MVC表單無法發布對象列表所以我有一個MVC ASP.NET應用程序有問題。本質上,我有一個視圖,它包含一個表單,它的內容被綁定到一個對象列表中。在這個循環中,它加載PartialView中正在循環的項?,F在一切都在運轉,直到形體被提交。當提交時,控制器將被發送一個空對象列表。下面的代碼證明了這些問題。家長意見:@model IEnumerable<PlanCompareViewModel>@using (Html.BeginForm("ComparePlans", "Plans", FormMethod.Post, new { id = "compareForm" })){<div>
@foreach (var planVM in Model)
{
@Html.Partial("_partialView", planVM)
}</div>}_ParalView:@model PlanCompareViewModel<div>
@Html.HiddenFor(p => p.PlanID)
@Html.HiddenFor(p => p.CurrentPlan)
@Html.CheckBoxFor(p => p.ShouldCompare)
<input type="submit" value="Compare"/></div>以下是上述代碼的類:PlanViewModel: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
}}問題在于控制器的作用。據我所知,它應該發布一個PlanCompareViewModel的可枚舉列表,但它是空的。在檢查發送的POST數據時,它發送的是正確的Params。如果我要將‘IEnDigable’改為‘FormCollection’,它包含正確的值。有人知道為什么綁定器沒有創建正確的對象嗎?我可以使用javascript繞過這個問題,但這違背了目的!任何幫助都將不勝感激!
- 2 回答
- 0 關注
- 669 瀏覽
添加回答
舉報
0/150
提交
取消