为什么心不是我的EditorTemplate的[HttpPost]绑定列表,但它呈现罚款[HTTPGET] [英] Why isnt my EditorTemplate binding a List on [HttpPost], but renders it fine on [HttpGet]?

查看:130
本文介绍了为什么心不是我的EditorTemplate的[HttpPost]绑定列表,但它呈现罚款[HTTPGET]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经上传我的code到引擎收录,这是链接:

I have uploaded my code to pastebin, this is the link:

http://pastebin.com/wBu9PP2x

当我提交表单,我使用的列表不绑定到我的ViewModel。

When i submit a form, the Lists that i use are not bound to my ViewModel.

但是,当我视图模型发送给视图,它使用EditorFor呈现罚款。我已阅读,使用EditorTemplates时,它应该使他们回发时自动绑定到视图模型适当命名列表。

But when i send the ViewModel to the view, it renders fine using 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.

HTML输出可以在这里看到:

The HTML output can be seen here:

http://pastebin.com/5KeyNXWC

请注意,从视图模型ShowQuestionViewModel,其中包含了一些字符串派生的。这些字符串得到很好的约束。

Notice that the ViewModel derives from ShowQuestionViewModel, which contains some strings. 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

在本教程中,MVC框架知道如何绑定一个视图模型的内部列表。

In the tutorial, the MVC framework knows how to bind lists inside of a ViewModel.

下面是一些调试输出:

控制器采用ShowQuestionViewModel作为参数:

Controller takes ShowQuestionViewModel as parameter:

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

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

控制器采用的FormCollection作为参数:

Controller takes FormCollection as parameter:

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

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

不同的控制器,它接受一个列表,并作为的FormCollection参数:

Different Controller that takes a List and FormCollection as parameter:

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

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

不要放弃我家伙!

谢谢!

我发现这个solutin自己。我忘了使用属性在ShowMatrixQuestionViewModel的行和列清单。此外,ActionController的无不会TryUpdateModel()绑定所以拜@Adam Tuliper以及休息。

I have found this solutin myself. I forgot to use Properties for the rows and columns list in the ShowMatrixQuestionViewModel. Also, the ActionController wont bind without TryUpdateModel() so thanks to @Adam Tuliper as well as the rest.

推荐答案

既然你提到的列表,你确定你的模型后回传包含了所有预期的项目?此外remembe的HTML佣工将使用的ModelState如果你正在显示的数据后门柱,而不是重定向到从和数据绑定。

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.

这篇关于为什么心不是我的EditorTemplate的[HttpPost]绑定列表,但它呈现罚款[HTTPGET]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆