我有以下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>
以下是我如何创建复杂类型对象
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");
}
它显示问题很好,但是当我试图将值发布到控制器时它为元素对象赋予null,尽管我能够得到Section的值但是模型中元素的null值
有人可以在这里帮助我,我怎样才能获得每个元素的发布值,其中包含不同类型的问题。