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

enter image description here

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
asp.net-mvc


source to share


No one has answered this question yet

Check out similar questions:

1356
Potentially dangerous Request.Form value was detected on the client
236
Error: "Failed to load type MvcApplication"
177
MVC4 DataType.Date EditorFor doesn't display date value in Chrome, fine in Internet Explorer
163
ASP.NET MVC 3 Razor - Adding Class to EditorFor
156
Conversion from datetime2 datatype to datetime datatype exceeded
117
HTML Attributes for Editor () in ASP.NET MVC
110
EditorFor () and html properties
ten
EditorFor () for complex type list (MVC)
1
Mail Complex Types for WebApi Client
0
Editor For data loss in a message



All Articles
Loading...
X
Show
Funny
Dev
Pics