1

My code:

public ActionResult Index()
{
    IndexViewModel indexViewModel = new IndexViewModel();
    indexViewModel.Questions = GetQuestions();

    //Look here, I need to associate a answer for each question, i make a array with the number of questions, this seems a good idea?
    indexViewModel.Answers = new Answer[indexViewModel.Questions.Count];

    return View(indexViewModel);
}

[HttpPost]
public ActionResult Index(IndexViewModel indexViewModel)
{
    if (ModelState.IsValid)
    {
        //The problem is here:
        //The **Questions** property in IndexViewModel comes empty
        //and I need to associate Questions and Answers

        ...

        context.SaveChanges();

        return RedirectToAction("Index");
    } else
    {
        indexViewModel.Questions = GetQuestions();
        indexViewModel.Answers = new Answer[indexViewModel.Questions.Count];

        return View(indexViewModel);
    }
}

private IList<Question> GetQuestions()
{
    return context.Question.ToList();
}

The problem is in code comments. Thanks.

Update:

My HTML:

@using (Html.BeginForm()) {
<fieldset>
    @Html.ValidationSummary(false, "Fix the erros:")

    @for (int i = 0; i < Model.Questions.Count; i++)
    {
        <div class="editor-label">
            @Html.DisplayFor(model => model.Questions[i].Description)
        </div>
        <div class="editor-field">
            @Html.LabelFor(model => model.Answers[i].Score)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(model => model.Answers[i].Score)
        </div>
    }

    <p>
        <input type="submit" value="Send" />
    </p>
</fieldset>
}
Acaz Souza
  • 8,311
  • 11
  • 54
  • 97
  • It depends on how you render your html form. Asp.net mvc default model binder accepts specific format for binding collections of objects. – Arnis Lapsa Jul 26 '11 at 11:55
  • MVC model-binds inputs. There are no input tags related to questions in your html markup. Quick and dirty solution would be to @Html.HiddenFor(x=>x.Questions[i].Description for each question field. – Arnis Lapsa Jul 26 '11 at 12:06
  • @Arnis I don't want a Dirty solution, if you could post a complete and clean solution, I will be very grateful. – Acaz Souza Jul 26 '11 at 12:11
  • 1
    It's dirty because You need to pass entity state through HTTP. I suspect that You are not modifying it at client side so there shouldn't be need to pass it to client in first place. I mean - isn't this about answering questions (in contrast to editing question descriptions)? Store Questions in session or data store and pass to client identifiers only. – Arnis Lapsa Jul 26 '11 at 12:20

1 Answers1

0

probably the format you are rendering the view is not compliant to the default binder:

http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx

If you can't change the format of the HTML you have to create your own custom binder

ASP.NET MVC - Custom model binder able to process arrays

Community
  • 1
  • 1
Massimiliano Peluso
  • 26,379
  • 6
  • 61
  • 70