HiddenInput 的 Asp.Net MVC ModedMetadata 问题
我在 dll 中有一个非常简单的视图模型,我想将其与主 Web mvc 项目分开。
我正在使用元数据属性装饰模型,这将帮助 ui 显示正确的表示(DisplayName、UIHint、DataType、ReadOnly 等),并且我希望稍后在不同的表示层(如 Silverlight)中重用此信息
。来自命名空间 System.ComponentModel.DataAnnotations 但我惊讶地发现 HiddenInput 是一个例外,我需要在我的视图模型 dll 中添加对 System.Web.Mvc 的引用。
是否有特殊原因不将其包含在其他属性中?
我尝试覆盖将 HiddenInput.ascx 放入 editortemplates 文件夹中的默认行为,但当我在视图中调用 html.EditorfForModel() 时,我仍然获得该字段的标签。
I have a very simple view-model in a dll that I want to keep separated from the main web mvc project.
I am decorating the model with metadata attributes that will help the ui display the correct presentation (DisplayName, UIHint, DataType, ReadOnly etc) and I would like to reuse this information with different presentation layers later (like Silverlight)
Most of the attributes are coming from the namespace System.ComponentModel.DataAnnotations
but I was surprised to discover that HiddenInput is an exception to that and I need to add a reference to System.Web.Mvc in my view-model dll.
Is there a particular reason not to have included that with the other attributes?
I tried to override the default behavior putting an HiddenInput.ascx in the editortemplates folder but I still get the label for the field when I call an html.EditorfForModel() in my view.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为未包含在 System.ComponentModel.DataAnnotations 中的原因是该程序集是 BCL 的一部分,并且在 ASP.NET MVC 之前就已存在。 Brad Wilson 写了一篇不错的 博客文章 涵盖了您可能会读到的 MVC 中的模型元数据。
也就是说,您可以使用
[UIHint]
属性如下:在
~/Views/Home/EditorTemplates/Hidden.ascx
中:现在您可以使用
<%= Html.EditorForModel() % >
在您的视图中,它将选择自定义模板。当然,首选使用
[HiddenInput]
,因为它可以减少您编写的代码,但如果您不想在项目中引用System.Web.Mvc
,您仍然可以使用UIHint
解决方法..I believe the reason for not being included in the
System.ComponentModel.DataAnnotations
is because this assembly is part of the BCL and existed before ASP.NET MVC. Brad Wilson wrote a nice blog post covering model metadata in MVC which you might read.This being said you could use the
[UIHint]
attribute like this:And in
~/Views/Home/EditorTemplates/Hidden.ascx
:Now you can use
<%= Html.EditorForModel() %>
in your view and it will pick the custom template.Of course using
[HiddenInput]
is preferred as it makes you write less code but in case you don't want to referenceSystem.Web.Mvc
in your project you still have theUIHint
workaround..