Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MVC表单无法发布对象列表_C#_Asp.net Mvc_Asp.net Mvc 4_Asp.net Mvc Partialview - Fatal编程技术网

C# MVC表单无法发布对象列表

C# MVC表单无法发布对象列表,c#,asp.net-mvc,asp.net-mvc-4,asp.net-mvc-partialview,C#,Asp.net Mvc,Asp.net Mvc 4,Asp.net Mvc Partialview,因此,我有一个MVC Asp.net应用程序存在问题。本质上,我有一个包含表单的视图,其内容绑定到一个对象列表。在这个循环中,它加载PartialView,其中包含循环的项目。现在一切正常,直到表格提交。当它被提交时,控制器被发送一个空的对象列表。下面的代码说明了这些问题 父视图: @model IEnumerable<PlanCompareViewModel> @using (Html.BeginForm("ComparePlans", "Plans", FormMethod.Po

因此,我有一个MVC Asp.net应用程序存在问题。本质上,我有一个包含表单的视图,其内容绑定到一个对象列表。在这个循环中,它加载PartialView,其中包含循环的项目。现在一切正常,直到表格提交。当它被提交时,控制器被发送一个空的对象列表。下面的代码说明了这些问题

父视图:

@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 IEnumerable
@使用(Html.BeginForm(“ComparePlans”、“Plans”、FormMethod.Post、new{id=“compareForm”}))
{
@foreach(模型中的var planVM)
{
@Html.Partial(“\u partialView”,planVM)
}
}
_部分观点:

@model PlanCompareViewModel
<div>
    @Html.HiddenFor(p => p.PlanID)
    @Html.HiddenFor(p => p.CurrentPlan)
    @Html.CheckBoxFor(p => p.ShouldCompare)
   <input type="submit" value="Compare"/>
</div>
@model plancomareviewmodel
@Html.HiddenFor(p=>p.PlanID)
@Html.HiddenFor(p=>p.CurrentPlan)
@CheckBoxFor(p=>p.ShouldCompare)
这些是上述代码的类:

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();
    }
}
公共类计划比较审阅模型
{
公共整数平面ID{get;set;}
公共计划CurrentPlan{get;set;}
公共bool应该比较{get;set;}
公共计划比较审阅模型(计划)
{
ShouldCompare=false;
PlanID=plan.PlanID;
当前计划=计划;
}
公共计划比较审查模型()
{
//TODO:完成成员初始化
}
公共静态IEnumerable CreatePlansVM(IEnumerable plans)
{
返回计划。选择(p=>newplanscompareviewmodel(p)).AsEnumerable();
}
}
控制器:

public class PlansController : MyBaseController
{
    [HttpPost]
    public ActionResult ComparePlans(IEnumerable<PlanCompareViewModel> model)
    {
         //the model passed into here is NULL
    }
}
公共类平面控制器:MyBaseController
{
[HttpPost]
公共行动结果比较计划(IEnumerable模型)
{
//传递到此处的模型为NULL
}
}
问题在于控制器的动作。据我所知,它应该发布一个PlanCompareViewModels的可枚举列表,但它是空的。在检查发送的post数据时,它正在发送正确的参数。如果我将“IEnumerable”更改为“FormCollection”,则它包含正确的值。有人知道为什么活页夹没有创建正确的对象吗?我可以使用javascript来解决这个问题,但这并没有达到目的!任何帮助都将不胜感激

请阅读以下内容:
您应该为html元素“name”属性设置标记,如
plancomareviewmodel[0]。PlanId
plancomareviewmodel[1]。PlanId
,使binder能够将它们解析为IEnumerable。

不要使用
@foreach(模型中的var planVM)
而使用
循环并使用索引呈现名称。

您的模型是
空的
,因为您向表单提供输入的方式意味着模型绑定器无法区分元素。现在,这个代码:

@foreach (var planVM in Model)
{
    @Html.Partial("_partialView", planVM)
}
没有为这些项目提供任何类型的索引。因此,它会重复生成如下HTML输出:

<input type="hidden" name="yourmodelprefix.PlanID" />
<input type="hidden" name="yourmodelprefix.CurrentPlan" />
<input type="checkbox" name="yourmodelprefix.ShouldCompare" />
最后,您的父视图简化为:

@model IEnumerable<PlanCompareViewModel>
@using (Html.BeginForm("ComparePlans", "Plans", FormMethod.Post, new { id = "compareForm" }))
{
<div>
    @Html.EditorForModel()
</div>
}
@model IEnumerable
@使用(Html.BeginForm(“ComparePlans”、“Plans”、FormMethod.Post、new{id=“compareForm”}))
{
@Html.EditorForModel()
}

DisplayTemplates
EditorTemplates
足够聪明,可以知道它们何时处理集合。这意味着它们将自动为表单元素生成正确的名称,包括索引,以便您能够正确地为绑定到集合建模。

谢谢,这个答案今天确实帮助了我。
@model PlanCompareViewModel
<div>
    @Html.HiddenFor(p => p.PlanID)
    @Html.HiddenFor(p => p.CurrentPlan)
    @Html.CheckBoxFor(p => p.ShouldCompare)
   <input type="submit" value="Compare"/>
</div>
@model IEnumerable<PlanCompareViewModel>
@using (Html.BeginForm("ComparePlans", "Plans", FormMethod.Post, new { id = "compareForm" }))
{
<div>
    @Html.EditorForModel()
</div>
}