在 ASP.NET MVC3 和 C# 中进行模型绑定的好资源吗?
我想知道模型绑定在 ASP.NET MVC3 中到底是如何工作的。由于我仍在等待我的 Professional ASP.NET MVC3 书籍,并且通过谷歌搜索找不到任何内容,因此您是我最后的希望。
我知道如何与简单对象执行绑定,但是当涉及到 ViewModel,尤其是嵌套 List
时,我无法执行绑定。
谢谢
弗朗西斯科
更新:
为了澄清,我的意思是从视图到操作方法的模型绑定,谢谢
I would like to know how exactly model binding works in ASP.NET MVC3. Since I am still waiting for my Professional ASP.NET MVC3 book and I cannot find anything by googling it, you are my last hope.
I know how to perform binding with simple objects but when it comes to ViewModels, especially with nested List<T>
, I am unable to perform binding.
Thanks
Francesco
UPDATE:
For clarification, I mean Model binding from View to Action Methods, thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个问题并不完全清楚,所以我将解决我认为您寻求帮助的问题。
如果视图模型实体具有
List
属性或其他一些可枚举属性,则它不会自动绑定到标记为 HttpPost 的操作方法中可用的结果模型实例。您只需找到一个位置来保存数据,或者只需在 Action 方法中重新查询它并更新发布的实例。
我发现的最可靠的方法是将数据序列化为 JSON 并将这些值放入隐藏的表单字段中,但当我这样做时,我的视图模型不再具有 List 属性,而是具有序列化属性。
这种困境通常迫使我重新评估表单帖子上可用数据的需求,在大多数情况下,这是因为我尝试在具有不同要求的视图之间重用视图模型。
The question is not completely clear, so I'll address what I think you are asking for help on.
In cases where a View Model entity has a property of
List<T>
or some other enumerable, it is not automatically bound to the resulting model instance that is available in the action method marked as HttpPost.You simply need to to find a place to persist the data, or, simply re-query for it in your Action method and update the posted instance.
The most reliable way I have found involves serializing the data to JSON and putting those values into hidden form fields, but when I do this, my view models no longer have the List property, but rather, the serialized properties.
This dilemma usually forces me to re-evaluate the need for the data to be available on form posts, and in most cases it is because I have tried to reuse a view model across views that have different requirements.
据我所知,mvc3 中的模型绑定没有发生任何变化,所以我想关于模型绑定的章节来自 Pro ASP.NET MVC V2 Framework仍然有效。
当您必须将嵌套列表绑定到操作参数时,我建议使用 javascript。
As far as I know there is no changes in model binding have been made in mvc3, so I suppose chapter about model binding from Pro ASP.NET MVC V2 Framework is still valid.
I recommend to use javascript when you have to bind nested lists to action parameter.