开发者

MVC default model binding to complex list

开发者 https://www.devze.com 2022-12-10 08:42 出处:网络
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

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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号