简体   繁体   English

Html.EditorFor()不支持数据注释

[英]Html.EditorFor() does not honor data annotations

I have a model with the following property: 我有一个具有以下属性的模型:

[Required]
[HiddenInput(DisplayValue = false)]
public override int Id { get; set;}

Now, it is my understanding that the html helpers are supposed to honor these data annotation attributes when rendering properties. 现在,我的理解是,在呈现属性时,html助手应该遵循这些数据注释属性。 However when I do 但是,当我这样做

@Html.EditorFor(m => m.Id) 

the following html is produced: 生成以下html:

<input class="text-box single-line" id="Id" name="Id" type="number" value="2">

I expect the field to be hidden but it is not. 我希望隐藏这个字段,但事实并非如此。 I have found another helper which DOES honor the annotation attributes: 我找到了另一个支持注释属性的助手:

@Html.Editor("Id")

The html produced by this sets the field to hidden as it should be: 由此产生的html将字段设置为隐藏,因为它应该是:

<input data-val="true" data-val-number="The field Id must be a number." data-val-required="The Id field is required." id="Id" name="Id" type="hidden" value="2">

As far as I can tell both helpers are from the namespace System.Web.Mvc.Html, implementations of both are from the System.Web.Mvc Assembly, version 5.2.3.0. 据我所知,两个助手都来自命名空间System.Web.Mvc.Html,两者的实现都来自System.Web.Mvc Assembly,版本5.2.3.0。

I would like to use the @Html.EditorFor() method, but I also need the data annotations. 我想使用@ Html.EditorFor()方法,但我还需要数据注释。

All ideas welcome 欢迎所有想法

Solved by @JakubJankowski in a comment. 由@JakubJankowski在评论中解决。 @Html.EditorFor() does not honor data annotations on overriden properties. @ Html.EditorFor()不支持覆盖属性上的数据注释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM