MVC 默认模型绑定到复杂列表
我在使用默认活页夹绑定到嵌套列表时遇到一些问题。我正在使用 linq to sql 并具有以下数据
结构竞赛问题< ComparisonQuestionChoices
我的 html 如下
<%= 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)%>
在我的控制器中,我尝试过
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Save([Bind(Prefix="Competition")]Competition competition)
{
}
这让我参加竞争,但没有子元素,
如果没有列表中的竞争前缀 int HTML 并单独绑定到每个集合,我会更成功,例如,
UpdateModel(competition,"Competition");
UpdateModel(competition.CompetitionQuestions,"competitionQuestions");
但我无法让它工作对于竞赛问题选择,因为它必须有两个前缀,我不知道如何声明
感谢收到任何帮助。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实证明,我的问题源于 .Net 3.5 中的分配方法的问题。在瞄准 .Net 4 后,绑定工作正常。这是一篇带有进一步解释的帖子。
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.