Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6080147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:01:43+00:00 2026-05-23T11:01:43+00:00

I use Data Annotations in my ASP.NET MVC 3 project to validate the model.

  • 0

I use Data Annotations in my ASP.NET MVC 3 project to validate the model. These are extremely convenient but currently they are magic to me. I read that data annotations do not throw exceptions. How then does MVC know to add validation errors to the model state dictionary? How does the failure to set a property on the model because of model validation bubble up to MVC if no exception is thrown? I always assumed that exceptions were thrown every time a property failed and that MVC model binding caught the exception and added it to the model state dictionary.

To test this I created a console application and added a sample class with a validation annotation to it:

public class MyObject
{
    [StringLength(10, MinimumLength=3)]
    public string Name { get; set; }
}

I then instantiated the object and tried to assign values to the Name property that were less than 3. The property assigned just fine, despite the annotation that says string length of less than 3 is not allowed.

    static void Main(string[] args)
    {
        MyObject mine = new MyObject();
        mine.Name = "hi";
        Console.WriteLine(mine.Name);
        Console.ReadLine();
    }

This little program writes out “hi” to the console. Why? I was expecting it to get angry when trying to set mine.Name to “hi”.

What am I missing?

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T11:01:43+00:00Added an answer on May 23, 2026 at 11:01 am

    You never call anything to validate the properties. The validation doesn’t happen magically on its own. from http://msdn.microsoft.com/en-us/library/dd901590%28v=vs.95%29.aspx

    Manually Validating Values

    When you do not use the DataGrid
    control to provide the interface for
    editing data, the validation
    attributes are not automatically
    applied. However, you can manually
    apply the validation test by using the
    Validator class. You can call the
    ValidateProperty method on the set
    accessor of a property to check the
    value against the validation
    attributes for the property. You must
    also set bothValidatesOnExceptions
    and NotifyOnValidationError properties
    to true when data binding to receive
    validation exceptions from validation
    attributes. For an example of manually
    applying validation, see the Data
    Binding Example below.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Data Annotations to validate my Model in ASP.NET MVC. This works
i have an asp.net mvc application, and i use data annotations for data validations
We've an ASP.Net MVC2 web app (SQL 2008 in backend). We use Data Annotations
I am using the Data Annotation Validator, outlined here: http://www.asp.net/learn/mvc/tutorial-39-cs.aspx The Data Annotations Model
I'm using ASP.NET MVC 3 code-first and I have added validation data annotations to
I'm trying to use Sharp Architecture to develop ASP.NET MVC 3 app. But I
In ASP.NET MVC we can use the System.ComponentModel.DataAnnotations namespace to assist with form/ model
We manage several ASP.NET MVC client web sites, which all use a data annotation
I need to add regular expression data annotation on asp.net MVC 3 model (
i was wondering if there is a way to use ASP.Net's Data annotation without

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.