MVC表單無(wú)法發(fā)布對(duì)象列表所以我有一個(gè)MVC ASP.NET應(yīng)用程序有問(wèn)題。本質(zhì)上,我有一個(gè)視圖,它包含一個(gè)表單,它的內(nèi)容被綁定到一個(gè)對(duì)象列表中。在這個(gè)循環(huán)中,它加載PartialView中正在循環(huán)的項(xiàng)?,F(xiàn)在一切都在運(yùn)轉(zhuǎn),直到形體被提交。當(dāng)提交時(shí),控制器將被發(fā)送一個(gè)空對(duì)象列表。下面的代碼證明了這些問(wèn)題。家長(zhǎng)意見(jiàn):@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>以下是上述代碼的類(lèi):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();
}}主計(jì)長(zhǎng):public class PlansController : MyBaseController{
[HttpPost]
public ActionResult ComparePlans(IEnumerable<PlanCompareViewModel> model)
{
//the model passed into here is NULL
}}問(wèn)題在于控制器的作用。據(jù)我所知,它應(yīng)該發(fā)布一個(gè)PlanCompareViewModel的可枚舉列表,但它是空的。在檢查發(fā)送的POST數(shù)據(jù)時(shí),它發(fā)送的是正確的Params。如果我要將‘IEnDigable’改為‘FormCollection’,它包含正確的值。有人知道為什么綁定器沒(méi)有創(chuàng)建正確的對(duì)象嗎?我可以使用javascript繞過(guò)這個(gè)問(wèn)題,但這違背了目的!任何幫助都將不勝感激!
- 2 回答
- 0 關(guān)注
- 666 瀏覽
添加回答
舉報(bào)
0/150
提交
取消