即使在mvc中删除后,必需的字段验证器仍然会触发 [英] Required field validator still firing even after removing in mvc

查看:55
本文介绍了即使在mvc中删除后,必需的字段验证器仍然会触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,

我有ViewModel,其中有一个属性:

Hi friends,
I had ViewModel, in which there is a property:

<br />
[Required(ErrorMessage = "Please choose Flat Discount or Discount %")]<br />
public char IsFlatIndicator { get; set; }





没有要求已经改变,不应该要求。所以我删除了所需的验证器。



问题:

仍然是必需的验证器被解雇了。



解决方案:



No the requirement has changed, it should not be required. So I removed the Required validator.

Problem:
Still the required validator is fired.

Solution:

DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;



我可以知道原因,为什么即使从模型中删除验证器也会被解雇?



提前谢谢


May I know the reason, why it is fired even after removing the validator from model?

Thanks in advance

推荐答案

我无法复制此行为。

对于我,因为我删除了模型中的验证代码,所以没有验证客户端也是该物业。



为了快速验证,我从

初学者关于验证模型的教程ASP.NET MVC中的数据和不显眼的客户端验证 [ ^ ]



我首先按原样运行代码,并验证FirstName上的验证是工作。然后我从ContactMetaData类中删除了FirstName上的注释:

I am not able to replicate this behaviour.
For me as I remove the validation code in Model, there is no validation at client side too for that property.

To verify it quickly, I downloaded code from
A Beginner's Tutorial on Validating Model Data and Unobtrusive Client side Validation in ASP.NET MVC[^]

I first ran the code as it is and verified that validation on FirstName is working. Then I removed annotations on FirstName as given below from ContactMetaData class:
  class ContactMetaData
    {
      
        public string FirstName { get; set; }

        [Required(ErrorMessage = "Last Name is required")]
        [StringLength(50, ErrorMessage = "Last Name length Should be less than 50")]
        public string LastName { get; set; }

        [Required(ErrorMessage = "Address is required")]
        [StringLength(100, ErrorMessage = "Address length Should be less than 100")]
        public string Address { get; set; }
.
.
.
.

           }





之后我重建应用程序...浏览器方面也没有验证。我也查看了html,发现FirstName删除了验证标签:



After that as I rebuild the app...there was no validation in browser side too. I looked into html too and found that validation tags were removed for "FirstName":

<form action="/Contact/Create" method="post">    <fieldset>
        <legend>Contact</legend>

        <div class="editor-label">
            <label for="FirstName">FirstName</label>
        </div>
        <div class="editor-field">
            <input class="text-box single-line" id="FirstName" name="FirstName" type="text" value="" />
            <span class="field-validation-valid" data-valmsg-for="FirstName" data-valmsg-replace="true"></span>
        </div>
.
.
.

</fieldset>





由于浏览器缓存,您可能会收到它...或者可能是其他内容。



请考虑重建并清除浏览器缓存,让我知道您的结果。您还可以尝试我已采取的上述步骤来验证和更新您的结果。



这里有一篇很好的文章解释内部:

http://rachelappel.com/asp-net-mvc/how-data-annotations-for-asp-net-mvc- validation-work / [ ^ ]





谢谢。



You may be getting it due to browser cache ...or may be something else.

Please consider to do rebuild and clear browser cache one more time and let me know your result. You can also try above steps I have taken to verify and update your results too.

There is a good article explaining internals here:
http://rachelappel.com/asp-net-mvc/how-data-annotations-for-asp-net-mvc-validation-work/[^]


Thanks.


这篇关于即使在mvc中删除后,必需的字段验证器仍然会触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆