EditorForModel和DisplayForModel不要为Expando对象的工作 [英] EditorForModel and DisplayForModel don't work on Expando objects
问题描述
或者至少,他们不会做什么我所期待的。我的行动方法是这样的:
Or at least, they don't do what I was expecting. My Action method looks like this:
Function Test() As ActionResult
Dim model As Object = New ExpandoObject()
model.FieldA = 123
model.FieldB = "This is a string"
model.FieldC = DateTime.Now
Return View(model)
End Function
和观点如下:
@ModelType Object
@Html.DisplayForModel
我期待它来生产如果模型是这些领域的一个真正的对象你会得到同样的事情,而是我得到这样的:
I was expecting it to produce the same thing you'd get if the model were a real object with those fields, but instead I get this:
[FieldA, 123][FieldB, This is a string][FieldC, 3/29/2012 12:10:24 PM]
EditorForModel做同样的事情。这是正确的行为,什么是实现,我正在寻找的行为的最好方式?
EditorForModel does the exact same thing. Is this the correct behavior, and what's the best way to implement the behavior that I'm looking for?
推荐答案
是的,这是正确的行为。默认的元数据提供商(DataAnnotationsModelMetadataProvider)使用反射来获取模型信息,那就是你得到这个结果的原因。
Yes, this is the correct behavior. Default metadata provider (DataAnnotationsModelMetadataProvider) uses reflection to get model information and that is the reason you get this result.
我建议创建一个自定义元数据提供商,将与动力学正常工作。尝试从该类继承 - <一个href=\"http://msdn.microsoft.com/en-us/library/system.web.mvc.dataannotationsmodelmetadataprovider.aspx\" rel=\"nofollow\">http://msdn.microsoft.com/en-us/library/system.web.mvc.dataannotationsmodelmetadataprovider.aspx.
I would suggest to create a custom metadata provider that will work correctly with dynamics. Try to inherit from this class - http://msdn.microsoft.com/en-us/library/system.web.mvc.dataannotationsmodelmetadataprovider.aspx.
这篇关于EditorForModel和DisplayForModel不要为Expando对象的工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!