在 MVC 应用程序中使用伙伴类元数据时,数据注释属性不起作用

发布于 2024-09-26 04:13:34 字数 877 浏览 0 评论 0原文

我发现 MVC 2 可以识别“伙伴类”类型的属性元数据,其中数据注释属性应用于“伙伴”元数据类,并且实际实体类上的 MetadataType 指向该伙伴类,如下所示。然而,如下所示,对呈现的 UI 产生任何影响的唯一属性似乎是 DisplayName。为什么 DataTypeRequiredReadOnly 等其他属性不起作用?即为什么我可以在只读字段中输入文本?当必填字段为空时,为什么我不会收到错误消息?为什么DataType属性没有明显效果?为什么 EditorForModel 不包含验证消息?

[MetadataType(typeof(CustomerMetadata))]
public partial class Customer
{
    public class CustomerMetadata
    {
        [ScaffoldColumn(false)]
        public object CustomerId { get; set; }

        [DisplayName("CustomerNo.")]
        [ReadOnly(true)]
        [Required(AllowEmptyStrings = false, ErrorMessage = "Customer No. is required.")]
        public object CustomerNo { get; set; }
    }
}

我发现无论我为每个模型属性使用显式 LabelForTextBoxFor,还是为整个模型使用单个 EditorForModel,行为都是相同的。

I have found hints that MVC 2 recognises the 'buddy class' type of property metadata, where data annotation attributes are applied to a 'buddy' metadata class, and the MetadataType on the actual entity class points to that buddy class, as below. However, as below, it seems the only attribute that makes any difference to the rendered UI is DisplayName. Why are the other attributes like DataType, Required, and ReadOnly not working? I.e. why can I enter text in a read only field? Why do I not get an error when a required field is empty? Why does the DataType attribute have no apparent effect? Why does EditorForModel not include validation messages?

[MetadataType(typeof(CustomerMetadata))]
public partial class Customer
{
    public class CustomerMetadata
    {
        [ScaffoldColumn(false)]
        public object CustomerId { get; set; }

        [DisplayName("CustomerNo.")]
        [ReadOnly(true)]
        [Required(AllowEmptyStrings = false, ErrorMessage = "Customer No. is required.")]
        public object CustomerNo { get; set; }
    }
}

I find behaviour the same whether I use an explicit LabelFor and TextBoxFor for each model property, or a single EditorForModel for the whole model.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请持续率性 2024-10-03 04:13:34
  1. 必需仅影响验证。
  2. Readonly 仅影响绑定。

仅当您使用 ValidationFor() 方法时才会输出 ErrorMessage 字符串。

  1. Required only affects validation.
  2. Readonly only affects binding.

The ErrorMessage string is only output when you use the ValidationFor() method.

旧情勿念 2024-10-03 04:13:34

因为我在视图中包含了一个 EnableClientValidation() 调用,所以我期望这些属性会导致客户端执行 Javascript 验证并显示验证消息。

事实证明,仅包含 EnableClientValidation() 还不够,还必须修改母版页(如果不使用母版页,则查看),以包含以下脚本:

<script src="../../Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcValidation.js" type="text/javascript"></script>

我不确定验证是否需要 jQuery,但我按照建议将其包含在内,现在一切正常。

Because I was including an EnableClientValidation() call in my view, I was expecting these attributes to cause client side, Javascript validation to be performed, and validation messages to be displayed.

It turns out that merely including EnableClientValidation() is not alone enough, and one also has to modify the master page (or view if you're not using a master page), to include the following scripts:

<script src="../../Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcValidation.js" type="text/javascript"></script>

I'm not sure the jQuery is required for validation or not, but I included it as advised and things work properly now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文