MVC 默认模型绑定到复杂列表

发布于 2024-08-09 16:17:39 字数 1222 浏览 5 评论 0原文

我在使用默认活页夹绑定到嵌套列表时遇到一些问题。我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

在梵高的星空下 2024-08-16 16:17:39

事实证明,我的问题源于 .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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文