1

View

@model Survey.Models.TakeSurveyViewModel
@{    
    Layout = "~/Views/Shared/_SiteLayout.cshtml";
}

<h2>SURVEY : @Model.Title</h2>
<h3>@Model.Description</h3>
<hr />

@using (Html.BeginForm("SubmitSurvey", "HomePage", FormMethod.Post, new { id = "surveyForm" }))
{
    for (int index = 0; index < Model.SurveyQuestions.Count; index++)
    {
        @* Editor Template - Null result *@ 
        @*SurveyQuestionModel item = Model.SurveyQuestions[index];
        @Html.EditorFor(x => item);*@

        <p>@Model.SurveyQuestions[index].QuestionText</p>
        @Html.DropDownListFor(item => Model.SurveyQuestions[index].OptionId, new SelectList(Model.SurveyQuestions[index].Options, "OptionId", "OptionText"), string.Empty)
    }
    <input type="submit" value="SubmitSurvey" />
}

ViewModel

public class TakeSurveyViewModel
    {
        public string Title { get; set; }
        public string Description { get; set; }
        public int SurveyId { get; set; }

        public List<SurveyQuestionModel> SurveyQuestions { get; set; } 

        public TakeSurveyViewModel() { }

        public TakeSurveyViewModel(int surveyId)
        {
            //Populate data - works ok.
        }
    }

DropDownList Model

public class SurveyQuestionModel
    {
        public int QuestionId { get; set; }
        public string QuestionText { get; set; }

        [Required(ErrorMessage = "Please select an option.")]        
        public int OptionId { get; set; }

        public IEnumerable<QuestionOption> Options { get; set; } 
    }

The page is rendering fine, with all the drop-downs with correct options. The id and name of each select is also unique -
id="SurveyQuestions_3__OptionId" name="SurveyQuestions[3].OptionId"

Controller Action

[HttpPost]
public ActionResult SubmitSurvey(TakeSurveyViewModel model)
{
     return !ModelState.IsValid ? TakeSurvey(model.SurveyId) : null;
}

But clicking on the submit button, controller action model is null.

Edit: Removed the 2x HTML.BeginForm

Edit 2: SurveyQuestions now has public setter. The issue seem to be still there. Please see this image:

enter image description here

2
  • 1
    You have 2 @Html.BeginForm? Was that intentional? Commented Feb 25, 2013 at 9:00
  • Removed the extra, still null on submit. Commented Feb 25, 2013 at 9:06

1 Answer 1

3

Is the problem that you have SurveyQuestions being a private set?

-old answer-
You have 2 x (Html.BeginForm(Html.BeginForm I once forgot to have my form within a using statement which did the same sort of thing.

Sign up to request clarification or add additional context in comments.

6 Comments

Just removed 1x BeginForm and its still null :(
ok, on making it public the empty option elements are getting created but still everything is null. 2nd edit - screenshot of issue.
When your form posts back, the only values that will be set for your Model will be the option list and the option id. Unless other items are present in your form, nothing else will be submitted back. Not even the option text.
A way around the option text is to store the selected text in a hidden field next to the dropdown. javascript onchange code...
Save it as a hidden field. Your model will only fill with what is posted from submitted form input elements.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.