Click here to Skip to main content
15,900,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
CSS
DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;

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

Thanks in advance
Posted

1 solution

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:
C#
  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; }
.
.
.
.

           }


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":
HTML
<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>


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.
 
Share this answer
 
v3
Comments
dhage.prashant01 27-Jan-15 22:46pm    
TQ, surely will try what you said and revert back :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900