MVC3 ModelBinder的为DynamicObject [英] MVC3 ModelBinder for DynamicObject

查看:131
本文介绍了MVC3 ModelBinder的为DynamicObject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想看看是否有一个示例项目,教程,contrib请分支机构或类似的东西,详细实施MVC3支持来自DynamicObject继承对象的自定义ModelBinder的。

I'm looking to see if there is a sample project, tutorial, contrib branch or anything like that that details implementing a custom ModelBinder for MVC3 to support objects inheriting from DynamicObject.

我有一个具有由数据库定义属性的动态数的域对象,并且这些可以在运行时间改变。要使用对象更容易,我做了我的类实现从DynamicObject继承和我通过[动态]关键字传递模型视图。

I have an domain object that has a dynamic number of properties as defined by the database, and these can change at run time. To make using the object easier I've made my class implementation inherit from DynamicObject and am passing the model to the view via the [dynamic] keyword.

全部为对象的动态特性的是在被称为属性的对象集合属性。我想创造出一系列的编辑模板来充实模型,最好让我所要做的就是沿着Html.EditorForModel()的线路的呼叫,它会动态生成UI。

All of the dynamic properties for the object are in a collection property on the object called "Attributes". I'd like to create a series of editor templates to flesh out the model, ideally so all I have to do is make a call along the lines of Html.EditorForModel() and it will dynamically build the UI.

问题是我没有多少运气找到一个能够ModelBinder的一个检查和DynamicObject脚手架了UI的实现(我认为这是这个正确的术语?)。

The problem is I'm not having much luck finding an implementation of a ModelBinder capable of inspecting a DynamicObject and scaffolding out the UI (I think that's the right term for this?).

我发现的IDictionary ModelBinder的项目,由罗伯特·埃尔南德斯(@hernandezrobert)上的 MVC3 Extensability (源以 HTTP: //mvcextensibility.$c$cplex.com/ ),但我还没有多少运气它适应我的目的。我不知道是否有人曾试图创建一个能够做什么,我描述的模型绑定?或者也许可以点我在正确的方向?

I found the IDictionary ModelBinder project done in one of the MVCConf videos done by Roberto Hernandez (@hernandezrobert) on MVC3 Extensability (source at http://mvcextensibility.codeplex.com/) but I haven't had much luck adapting it to my purposes. I was wondering if anyone else has tried to create a model binder capable of doing what I'm describing? or could maybe point me in the right direction?

推荐答案

我是能够解决通过建立一个普通的类,作为我DanamicObject模型,并有我的物业我的情况被储存像这样

I was able to solve my situation by setting up a normal class to serve as the Model for my DanamicObject, and having my properties be stored like so

IList<DynamicProperty> DynamicProperties { get; set; }

我创建了DynamicObject自定义视图,并在该视图我叫助手之一,以显示DynamicProperties财产。这让MVC迭代通过收集和呈现每个属性。接下来我有,我可根据需要使用呈现属性DynamicProperty景色。最关键的是,你还必须使包含特定属性的主键的隐藏字段。该模型绑定器在这里MVC3做一个更好的工作比在MVC2做,所以它会呈现数组索引的字段名的一部分,这样每个属性的主键和值上取得成功的正确提交配对。您可能会想专门创建一个视图模型为提交的数据,我有问题时,我试图用在细节上使用了相同的模型类/编辑的意见,因为我只渲染领域,使他们时,我必将失踪的一个子集同样的模型类上回发。

I created a custom view for the DynamicObject, and in that view I called one of the helpers to display the DynamicProperties property. This lets MVC iterate through the collection and render each property. Next I have a view for the DynamicProperty that I use to render the property as needed. The key is you also have to render a hidden field containing the primary key of that specific attribute. The ModelBinder in MVC3 does a much better job here than it did in MVc2, so it will render the array index as part of the field's name so that the primary key and value of each property get paired up correctly on the submit. You'll likely want to create a ViewModel specifically for the submitted data, I had issues when I tried to use the same model class used in the details/edit views because I only rendered a subset of fields so they were missing when I bound to the same Model class on postback.

您可以处理保存,但是你会正常,但也有一些注意事项securitywise这种类型的对象。因为属性的数目是动态的,也没有办法来确保相同的数量的字段被提交,因为最初呈现。用户可以注入自己的,或者更糟糕的领域你可能已经明确排除添加属性。该AntiForgeryToken将prevent这些排序提交的发生,从您的域名之外,但通过像jQuery库提供DOM操作的易用性和普及,跨站回发是不是唯一的顾虑,我不知道,如果AntiForgeryToken将占这一点,但我对此表示怀疑。

You can handle the save however you would normally but there are a few considerations securitywise for this type of object. Because the number of attributes is dynamic, there is no way to ensure that the same number of fields are submitted as were originally rendered. The user could inject their own, or worse add in fields for a property you may have explicitly excluded. The AntiForgeryToken would prevent these sort of submissions from happened outside of your domain but with the ease and popularity of DOM manipulation provided by libraries like jQuery, cross site postbacks isn't the only concerns and I don't know if the AntiForgeryToken will account for this but I doubt it.

该方法被证明是比试图从DynamicObject继承,实施GetDynamicMemberNames,并作出自定义的模型绑定器使用回绕更容易。

This approach proved to be easier than trying to inherit from DynamicObject, implement GetDynamicMemberNames, and making a custom ModelBinder to use wrap around it.

我也不过创建自定义ModelMetaData和验证提供者来处理这些问题,因为属性不强类型,所以MVC没有任何注解使用。

I did however create a custom ModelMetaData and Validation Provider to handle those aspects, since the properties are not strongly typed, so MVC didn't have any annotations to use.

这篇关于MVC3 ModelBinder的为DynamicObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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