ASP.NET MVC:访问视图上的 ViewModel 属性
有什么方法可以从视图访问 ViewModel 属性上的任何属性(无论是数据注释属性、验证属性还是自定义属性)?我想在其属性具有 [Required] 属性的字段旁边添加一些必填指示符。
例如,如果我的 ViewModel 如下所示:
public class MyViewModel
{
[Required]
public int MyRequiredField { get; set; }
}
我想在 EditorFor 模板中执行以下操作:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<int?>" %>
<div class="label-container">
<%: Html.Label("") %>
<% if (PROPERTY_HAS_REQUIRED_ATTRIBUTE) { %>
<span class="required">*</span>
<% } %>
</div>
<div class="field-container">
<%: Html.TextBox("") %>
<%: Html.ValidationMessage("") %>
</div>
Is there any way to access any attributes (be it data annotation attributes, validation attributes or custom attributes) on ViewModel properties from the view? One of the things I would like to add a little required indicator next to fields whose property has a [Required] attribute.
For example if my ViewModel looked like this:
public class MyViewModel
{
[Required]
public int MyRequiredField { get; set; }
}
I would want to do something in the EditorFor template like so:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<int?>" %>
<div class="label-container">
<%: Html.Label("") %>
<% if (PROPERTY_HAS_REQUIRED_ATTRIBUTE) { %>
<span class="required">*</span>
<% } %>
</div>
<div class="field-container">
<%: Html.TextBox("") %>
<%: Html.ValidationMessage("") %>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您要查找的信息位于
ViewData.ModelMetadata
中。 Brad Wilson 关于模板的 博客文章系列 应该解释这一切,特别是 模型元数据。至于其他 ValidationAttributes,您可以通过
ModelMetadata.GetValidators()
方法。ModelMetadata.IsRequired
会告诉您RequiredAttribute
是否需要复杂类型(或包装在Nullable
中的值类型),但它将为您提供不可为空的值类型的误报(因为它们是隐式必需的)。您可以通过以下方法解决此问题:注意:您需要使用
!metadata.ModelType.IsValueType
而不是model.IsComplexType
,因为ModelMetadata.IsComplexType
code> 返回 false,因为 MVC 不认为是复杂类型,其中包括字符串。The information you're looking for is in
ViewData.ModelMetadata
. Brad Wilson's blog post series on Templates should explain it all, especially the post on ModelMetadata.As far as the other ValidationAttributes go, you can access them via the
ModelMetadata.GetValidators()
method.ModelMetadata.IsRequired
will tell you if a complex type (or value type wrapped inNullable<T>
) is required by aRequiredAttribute
, but it will give you false positives for value types that are not nullable (because they are implicitly required). You can work around this with the following:Note: You need to use
!metadata.ModelType.IsValueType
instead ofmodel.IsComplexType
, becauseModelMetadata.IsComplexType
returns false for MVC does not consider to be a complex type, which includes strings.我建议不要这样做,因为您在视图中添加逻辑,这是一种不好的做法。
为什么不创建一个HtmlHelper或者LabelExtension,你可以在方法内部调用ModelMetaProvider并查看该属性是否有Required属性装饰?
I would suggest not doing that way because you're adding logic in the view which is a bad practice.
Why don't you create a HtmlHelper or LabelExtension, you can call ModelMetaProvider inside the method and find out whether the property has Required attribute decorated?