MVC default model binding to complex list

629 views Asked by At

I'm having some trouble binding to a nested list with the default binder. I am using linq to sql and have the following data structure

Competition < CompetitionQuestions < CompetitionQuestionChoices

my html is as follows

<%= Html.Hidden("Competition.Id",Model.Competition.Id) %>
 <%=Html.TextBox("Competition.CompetitionName", Model.Competition.CompetitionName )%>

<%= Html.TextBox("Competion.CompetitionQuestions[0].Id", Model.CompetitionQuestion.Id)%>
<%= Html.TextBox("Competion.CompetitionQuestions[0].Question", Model.CompetitionQuestion.Question )%>

<%= Html.TextBox("Competion.CompetitionQuestions[0].CompetitionQuestionChoices[0].Id", Model.CompetitionQuestionChoices.Id)%>

<%= Html.TextBox("Competion.CompetitionQuestions[0].CompetitionQuestionChoices[0].Choice", Model.CompetitionQuestionChoices.Choice)%>

In my controller I have tried

[AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Save([Bind(Prefix="Competition")]Competition competition)
{

}

which gets me a competition but no child elements

I have been more succesfull without the Competition prefix on the Lists int the HTML and binding to each collection individually e.g.

UpdateModel(competition,"Competition");
UpdateModel(competition.CompetitionQuestions,"competitionQuestions");

but I cant get this to work for competitionQuestionChoices as it has to have two prefixes and I'm not sure how to declare

Any help is gratefully received.

1

There are 1 answers

0
Joe On BEST ANSWER

it turns out my problems were stemming from an issue with the assign method in .Net 3.5 After targeting .Net 4 the bind worked correctly. Here is a post with further explanation.