Complex type of editor for publishing values
I have the following chtml
@model Licensure.Business.Survey.Survey
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
@using (Html.BeginForm())
{
@Html.EditorForModel()
}
I created the following editor template for complex type
@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>
Following is the way to create complex type object
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");
}
Its question display is fine, but when I try to post the values for the controller, it gives null for the elements element, although I can get the section values, but null for the elements in the model
Can someone please help me here how can I get the posted values of each item that has different types of questions in it.
+3
source to share
No one has answered this question yet
Check out similar questions: