Asp.net mvc 不过帐值的编辑器的复杂类型

Asp.net mvc 不过帐值的编辑器的复杂类型,asp.net-mvc,Asp.net Mvc,我有以下chtml @model Licensure.Business.Survey.Survey @{ Layout = "~/Views/Shared/_Layout.cshtml"; } @using (Html.BeginForm()) { @Html.EditorForModel() } 我为复杂类型的模板创建了以下编辑器 @model Licensure.Business.Survey.Survey <div> <div class

我有以下chtml

@model Licensure.Business.Survey.Survey
@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}


@using (Html.BeginForm()) 
{

@Html.EditorForModel()  

}
我为复杂类型的模板创建了以下编辑器

@model Licensure.Business.Survey.Survey
<div>

    <div class="container">


            @for (var i = 0; i < Model.Sections.Count; i++)
            {             
               @Html.EditorFor(model => Model.Sections[i])         
            }

            @Html.HiddenFor(m => m.SurveyId)

        <input class="btn btn-success" type="submit" value="Save Survey" /> 

    </div>
</div>
@model Licensure.Business.Survey.Survey
@对于(var i=0;imodel.Sections[i])
}
@Html.HiddenFor(m=>m.SurveyId)
下面是我如何创建复杂类型对象

   var survey = new Survey() {
                    Name = "Survey for Testing",
                    Sections = new List<Section>()
                };

                var section = new List<Section>();

                var section1 = new Section() {
                    Name = "Section A",
                    ElementType = ElementType.Section,
                    Elements = new List<Element>(),
                    FieldAssemblyName = section.GetType().Assembly.FullName,
                    FieldClassName = section.GetType().FullName 
                };

                var section2 = new Section()
                {
                    Name = "Section B",
                    ElementType = ElementType.Section,
                    Elements = new List<Element>(),
                    FieldAssemblyName = section.GetType().Assembly.FullName,
                    FieldClassName = section.GetType().FullName 
                };

                var Question1 = new QuestionYesNo() {
                    QuestionText = "Do you like apples?",
                    ElementType = ElementType.AnswerableQuestion,
                    Id = 1
                };

                var Question2 = new QuestionYesNo()
                {
                    QuestionText = "Do you like getting hit in the face?",
                    ElementType = ElementType.AnswerableQuestion,
                    Id = 2
                };

                var Question3 = new QuestionMultipleSelect()
                {
                    QuestionText = "Do you like getting hit in the face?",
                    ElementType = ElementType.AnswerableQuestion,
                    Options = new List<QuestionOption>(),
                    Answers = new List<QuestionOption>(),
                    ToolTip = new QuestionToolTip(){Text="You need more info?"},
                    Id = 3
                };

                var Question3Options = new List<QuestionOption>();
                Question3Options.Add(new QuestionOption(){ DisplayText = "Option 1", Value = "1"});
                Question3Options.Add(new QuestionOption() { DisplayText = "Option 3", Value = "2" });
                Question3Options.Add(new QuestionOption() { DisplayText = "Option 4", Value = "3" });
                Question3Options.Add(new QuestionOption() { DisplayText = "Option 5", Value = "4" });
                Question3Options.Add(new QuestionOption() { DisplayText = "Option 5", Value = "5" });

                Question3.Options = Question3Options;

                section1.Elements.Add(Question1);
                section2.Elements.Add(Question2);
                section2.Elements.Add(Question3);

                survey.Sections.Add(section1);
                survey.Sections.Add(section2);

                return View(survey);




       public ActionResult Index(Survey model)
                {
                    return RedirectToAction("Index", "Home");
                }
var调查=新调查(){
Name=“测试调查”,
节=新列表()
};
var节=新列表();
var section1=新节(){
Name=“A节”,
ElementType=ElementType.Section,
元素=新列表(),
FieldAssemblyName=section.GetType().Assembly.FullName,
FieldClassName=section.GetType().FullName
};
var Section 2=新节()
{
Name=“B节”,
ElementType=ElementType.Section,
元素=新列表(),
FieldAssemblyName=section.GetType().Assembly.FullName,
FieldClassName=section.GetType().FullName
};
var Question1=新问题是否(){
QuestionText=“你喜欢苹果吗?”,
ElementType=ElementType.AnswerableQuestion,
Id=1
};
var Question2=新问题是否()
{
QuestionText=“你喜欢被人打在脸上吗?”,
ElementType=ElementType.AnswerableQuestion,
Id=2
};
var Question3=新问题multipleselect()
{
QuestionText=“你喜欢被人打在脸上吗?”,
ElementType=ElementType.AnswerableQuestion,
选项=新列表(),
答案=新列表(),
ToolTip=新问题ToolTip(){Text=“您需要更多信息吗?”},
Id=3
};
var Question3Options=新列表();
Question3Options.Add(新的QuestionOption(){DisplayText=“Option 1”,Value=“1”});
添加(新的QuestionOption(){DisplayText=“Option 3”,Value=“2”});
添加(新的QuestionOption(){DisplayText=“Option 4”,Value=“3”});
添加(新的QuestionOption(){DisplayText=“Option 5”,Value=“4”});
添加(新的QuestionOption(){DisplayText=“Option 5”,Value=“5”});
问题3.选项=问题3选项;
第1节.要素.添加(问题1);
第2节.要素.添加(问题2);
第2节.要素.添加(问题3);
调查.章节.增补(第1节);
调查。章节。添加(第2节);
返回视图(调查);
公共行动结果指数(调查模型)
{
返回重定向到操作(“索引”、“主页”);
}
它很好地显示了这个问题,但当我试图将值发布到控制器时,它为elements对象提供了null,尽管我能够获得模型中元素的Section值,但为null值


有人能在这里帮助我如何获得每个元素的发布值,其中包含不同类型的问题。

您是否有
编辑器或模板
用于
部分的类型
@html.EditorFor(model=>model.Sections[i])
生成的html是什么?我有调查类型的editortemplate,其中有部分的for循环。根据需要显示相关代码的元素,生成不同类型的问题,如复选框、radiobutton或dropdownlist。您所显示的只是一个完全无关的代码块,它显示了如何填充
部分(删除它!)。显示您的模型和视图。如果
Elements
null
这是因为您的模型定义或视图中有错误,请重新考虑,不要删除该代码块-似乎
Element
是一个基类,您正在尝试将派生类型(
QuestionYesNo
QuestionMultipleSelect
)添加到
列表()
除非您有一个自定义的
ModelBinder
,否则这将不起作用。我刚刚想起了您刚才提到的另一个问题。如果您想开始创建一个抽象的
ModelBinder
,我建议您先创建一个抽象的
ModelBinder
,但它很复杂,而且当它涉及到作为集合的属性时,会变得更复杂。如果您准备在视图中分组您的问题(即所有“是/否”问题,然后是所有“多项选择”问题等),这可以通过视图模型轻松解决,并且您可以摆脱那些可笑的属性,如
ElementType
FieldAssemblyName
FieldClassName