如何注入自定义 ModelMetadata 属性并在 Html 帮助程序中使用它们
这个想法(简化)是在字典中包含 ViewModel 中用户可定义的属性。大致思路如下:
public class MyViewModel
{
[Required]
public string Name { get; set; }
[DisplayName("User address")]
public string Address { get; set; }
// ...
public IDictionary<string, string> MetaData { get; set; }
}
假设 MetaData 包含几个附加属性:PhoneNumber
、Email
等,您可以使用 myViewModel 访问这些属性.MetaData[“电话号码”]
。
我想做的是能够在视图端的 Html 帮助器中使用这些附加元数据属性,就像我使用普通属性一样。
因此,除了使用标准属性之外:
Html.TextBox("Name")
我还想使用这些附加属性:
Html.TextBox("PhoneNumber")
我的研究引导我继承 DataAnnotationsModelMetadataProvider
(因为还需要支持标准属性的标准 DataAnnotations 属性)并且试图弄清楚到底要重写什么,以便注入附加属性作为附加 ModelMetadata 元素,但我有点卡住了。
我走在正确的道路上吗?还有什么额外的指针可以帮助我吗?
谢谢
The idea (simplified) is to have user definable properties in ViewModel contained in a dictionary. Something along these lines:
public class MyViewModel
{
[Required]
public string Name { get; set; }
[DisplayName("User address")]
public string Address { get; set; }
// ...
public IDictionary<string, string> MetaData { get; set; }
}
Let's say that MetaData contains several additional properties: PhoneNumber
, Email
, etc. that you can access with myViewModel.MetaData["PhoneNumber"]
.
What I would like to do is to be able to use those additional MetaData properties in Html helpers on View side, just like I would use normal properties.
So, in addition to using standard properties as:
Html.TextBox("Name")
I would also like to use those additional properties:
Html.TextBox("PhoneNumber")
My research lead me to inheriting from DataAnnotationsModelMetadataProvider
(since it's necessary to also support standard DataAnnotations attributes for standard properties) and trying to figure out what exactly to override there in order to inject additional properties as additional ModelMetadata elements, but I'm kind of stuck.
Am I on the right path? Any additional pointer that could help me here?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
另一种选择可能是构造一个类似于 MVC 3 中的 ViewBag/ViewData 的动态对象。您将拥有一个可以通过
Model.MetaData.Foo
和Foo
访问的对象实际上会映射到你字典中的一个键。支持 ViewBag 对象的类型是 System.Web.Mvc .DynamicViewDataDictionary;这个类是内部的并且是密封的,因此您必须对其进行自定义实现(除非有我不知道的更好的选择)。快速浏览一下 MVC 3 源代码可以看出:
与修改 ModelMetadataProvider 相比,此解决方案的一个可能优点是您不必花时间为您的场景构建所有自定义组件 - 默认提供程序应该 足够了。
An alternative option might be to construct a dynamic object similar to ViewBag/ViewData in MVC 3. You would have an object which you could access via
Model.MetaData.Foo
andFoo
would actually map to a key in your dictionary.The type which backs the ViewBag object is System.Web.Mvc.DynamicViewDataDictionary; this class is internal and sealed so you would have to make a custom implementation of it (unless there's a better option I'm unaware of). A quick glance at the MVC 3 sources furnished this:
One possible advantage to this solution over modifying the ModelMetadataProvider is that you wouldn't have to spend time building all the custom components for your scenario- the default providers should be sufficient.