我可以通过 UIHint 控制参数发送 SelectList 吗?
我可以通过数据注释发送 SelectList 吗?就像......
[UIHint("DropDownList", "", new SelectList(new[] {"one","two","three"}))]
public virtual int? OptionID { get; set; }
我不明白语法,但这似乎是可能的。如果是这样,我如何从编辑器模板访问它?
如果没有,我如何动态地将 SelectList 发送到 DropDownList 编辑器模板?我特别想避免为每个 SelectList 制作单独的模板 - 我有太多的模板。谢谢
编辑:我正在研究第二个选项(反射),因为我认为它可能比覆盖那个 15 音节怪物 DataAnnotationsModelMetadataProvider 更直接。
Can I send a SelectList through a Data Annotation? Something like...
[UIHint("DropDownList", "", new SelectList(new[] {"one","two","three"}))]
public virtual int? OptionID { get; set; }
I don't understand the syntax but this seems possible. If so, how do I access it from an editor template?
If not, how could I dynamically send a SelectList to a DropDownList Editor Template? I specifically would like to avoid making a separate template for every SelectList - I have too many of them. Thanks
EDIT: I'm working on the second option (Reflection) because I thought it might be more direct than overriding that 15-syllable monster, the DataAnnotationsModelMetadataProvider.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能想看看这篇博客文章:
http:// /mikevdm.com/BlogEntry/Key/Using-UIHint-With-ControlParameters-in-MVC
这是相关引用:
在我的应用程序中,我的所有下拉列表都是可为空的布尔值(是、否、未设置)或枚举。我采取了为每个模板制作一个单独的模板的方法,但使用辅助方法使每个模板中的代码非常少。
例如,我有一个名为 Level 的模板(其中
Level
是一个枚举):该代码只是一些使用加上...
我将这些扩展方法用于
ToSelectList
:也许您可以使用控制参数来简化为单个模板,但您肯定需要编写自己的
DataAnnotationsModelMetadataProvider
,如博客文章中所示。You might want to take a look at this blog post:
http://mikevdm.com/BlogEntry/Key/Using-UIHint-With-ControlParameters-in-MVC
Here's a relevant quote:
In my app, all my drop down lists were either nullable bools (Yes, No, not set) or enums. I took the route of making a separate template for each one, but using helper methods to make the code within each template very minimal.
For example, I have a template called Level (where
Level
is an enum):The code is just a couple usings plus....
I use these extension methods for
ToSelectList
:Perhaps you could use control parameters to get down to a single template, but you'll definitely need to write your own
DataAnnotationsModelMetadataProvider
, as indicated in the blog post.