MVC3 ModelBinding到集合调回与指数的差距 [英] MVC3 ModelBinding to a collection posted back with index gaps

查看:127
本文介绍了MVC3 ModelBinding到集合调回与指数的差距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我在视图中使用EditFor功能呈现在我的模型对象的集合,我有一个EditorTemplate负责实际渲染每个对象。

I have a collection of objects on my Model that I'm rendering in a View by using EditFor function, and I have an EditorTemplate which is responsible for actually rendering each object.

@Html.EditorFor(model => model.MyObjects)

这一直运作良好,而现在,当你检查HTML,我的文本框都与模型属性pfixed $ P $,随后他们来自集合的索引。

This has worked well for a while now, and when you check the html, my text boxes are prefixed with the model property, followed by the index of the collection they came from.

<input class="text-box single-line" id="MyObjects_2__SomeProperty" 
name="MyObjects[2].SomeProperty" type="Text" value="" />

不过我在集合的元数据模型使用ShowForEdit和ShowForDisplay属性最近开始,并在如果ShowForEdit是不是真的我的编辑模板的第一行,我只是跳过它。

However I've recently started using the ShowForEdit and ShowForDisplay properties in the model metadata for the collection, and in the first line of my editor template if the ShowForEdit is not true, I just skip it.

@if (!ViewData.ModelMetadata.ShowForEdit)
{
    return;
}

但由于这些都是在HTML索引,当我试图挽救这个集合恢复到视图模型通过回传,它失败,因为在索引号的依赖。缺少指数后,集合中的每个项目从我的视图模型缺少当我检查它的价值。

But because these are all indexed in the html, when I try to save this collection back to the viewmodel via a postback, it fails because of a reliance on the indexing numbers. Every item in the collection after the missing index is missing from my view model when I check it's value.

在这种情况下,它实际上是我的了,我逃课,因为我不希望它是在编辑视图中可见,但正因为如此,当我回发集合中的第一项在HTML的第一个指数是1(而不是0喜欢它通常会),但是当您试图保存更改,这是一个问题。这也是一个问题,使用JavaScript改变DOM时。

In this case it's actually my first item in the collection that I'm skipping since I don't want it to be visible on the edit view, but because of this when I postback the first index in the html is 1 (instead of 0 like it normally would be), but this is a problem when you try to save the changes. This is also a problem when altering the DOM using javascript.

有没有其他人遇到默认的模型绑定的数据读取能力的问题调回当HTML重新presented集合中的一个或多个索引不present?

Has anyone else encountered a problem with the default model binder's ability to read data posted back when one or more indexes in the html represented collection are not present?

是否有处理这个问题的模型粘合剂?

Are there model binders that handle this problem?

推荐答案

有一些非常好的博客文章,让您modelbind到列表,而不需要提供零基于相邻索引。 PLZ看看
http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/

http://zahidadeel.blogspot.com/2011/05/master-detail-form-in-aspnet-mvc-3-ii.html

此外,如果你有兴趣MVVM模式和淘汰赛JS可以通过的史蒂夫·桑德森结果
更多阅读把编辑varibale长名单MVC样式在谷歌,它会给ü一打有用的链接

There are some very good blog posts that allow you to modelbind to a list without the need to provide zero based contiguous index. plz have a look at http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/
http://zahidadeel.blogspot.com/2011/05/master-detail-form-in-aspnet-mvc-3-ii.html
Furthermore, if you are interested in MVVM pattern and knockout js you can check this great work by steve sanderson
For more reading put "editing varibale length list mvc style" in google and it will give u a dozen useful links

这篇关于MVC3 ModelBinding到集合调回与指数的差距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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