简体   繁体   English

为什么我的EditorTemplate不能在[HttpPost]上绑定列表,但在[HttpGet]上可以正常显示呢?

[英]Why isnt my EditorTemplate binding a List on [HttpPost], but renders it fine on [HttpGet]?

I have uploaded my code to pastebin, this is the link: 我已将我的代码上传到pastebin,这是链接:

http://pastebin.com/wBu9PP2x http://pastebin.com/wBu9PP2x

When i submit a form, the Lists that i use are not bound to my ViewModel. 当我提交表单时,我使用的列表未绑定到我的ViewModel。

But when i send the ViewModel to the view, it renders fine using EditorFor. 但是,当我将ViewModel发送到视图时,使用EditorFor可以很好地呈现。 I have read that when using EditorTemplates, it is supposed to name the List appropriately so that they are bound to the ViewModel automatically upon postback. 我已经读过,使用EditorTemplates时,应该适当命名List,以便它们在回发时自动绑定到ViewModel。

The HTML output can be seen here: HTML输出可以在这里看到:

http://pastebin.com/5KeyNXWC http://pastebin.com/5KeyNXWC

Notice that the ViewModel derives from ShowQuestionViewModel, which contains some strings. 请注意,ViewModel派生自ShowQuestionViewModel,其中包含一些字符串。 These strings get bound perfectly. 这些字符串完美地绑定在一起。

This is the tutorial i have been following: 这是我一直关注的教程:

http://jarrettmeyer.com/post/2995732471/nested-collection-models-in-asp-net-mvc-3 http://jarrettmeyer.com/post/2995732471/nested-collection-models-in-asp-net-mvc-3

In the tutorial, the MVC framework knows how to bind lists inside of a ViewModel. 在本教程中,MVC框架知道如何在ViewModel中绑定列表。

Here are some debugger outputs: 这是一些调试器输出:

Controller takes ShowQuestionViewModel as parameter: 控制器将ShowQuestionViewModel作为参数:

http://imageshack.us/photo/my-images/803/debug.jpg http://imageshack.us/photo/my-images/803/debug.jpg

Controller takes FormCollection as parameter: 控制器将FormCollection作为参数:

http://imageshack.us/photo/my-images/542/formcollection.png http://imageshack.us/photo/my-images/542/formcollection.png

Different Controller that takes a List and FormCollection as parameter: 将List和FormCollection作为参数的另一个Controller:

http://imageshack.us/photo/my-images/685/listtest.png http://imageshack.us/photo/my-images/685/listtest.png

Dont give up on me guys!! 不要放弃我!

Thanks! 谢谢!

Solution

I have found this solutin myself. 我自己发现了这种甜味剂。 I forgot to use Properties for the rows and columns list in the ShowMatrixQuestionViewModel. 我忘了对ShowMatrixQuestionViewModel中的行和列列表使用Properties。 Also, the ActionController wont bind without TryUpdateModel() so thanks to @Adam Tuliper as well as the rest. 此外,如果没有TryUpdateModel(),ActionController将不会绑定,因此感谢@Adam Tuliper以及其他人。

Since you mentioned lists are you sure your model Upon postback contains all of the expected items? 既然您提到了列表,您确定您的模型“回发时”包含所有预期项目吗? Also remembe the HTML helpers will use modelstate to bind data from as well if you are showing data after a post and not redirecting. 另外请记住,如果您在发布后显示数据而不进行重定向,则HTML帮助器还将使用modelstate绑定数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM