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

  • Home
  • SEARCH
  • 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 8029017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:18:37+00:00 2026-06-05T00:18:37+00:00

In an ASP.NET MVC3 app I have a model that represents a user address

  • 0

In an ASP.NET MVC3 app I have a model that represents a user address with the typical Name, StreetAddress 1 & 2, City, Region, PostalCode and Country properties. The model currently has DataAnnotation attributes that apply to US addresses. I now need to support international addresses that will have different validation and messages depending on the Country value that is included in the model. How do I define and override the existing US DataAnnotation attribute values when the country is something like India or Japan instead of US?

For example the existing PostalCode property is defined as this:

    private string _postalCode;
    [StringLength(10, ErrorMessage = "Zip Code maximum length 10 characters")]
    [Display(Name = "Zip Code")]
    [Required(ErrorMessage = "Zip Code is required")]
    [RegularExpression(@"^\d{5}(-\d{4})?$", ErrorMessage = "Invalid Zip Code")]
    public string PostalCode
    {
        get { return _postalCode; }
        set
        {
            if (_postalCode != value)
            {
                _postalCode = value;
            }
        }
    }

I know if I had a specific India address model then the postal code would look something like this:

    private string _postalCode;
    [StringLength(6, ErrorMessage = "Postal Code maximum length 6 characters")]
    [Display(Name = "Postal Code")]
    [Required(ErrorMessage = "Postal Code is required")]
    [RegularExpression(@"^([0-9]{6})$", ErrorMessage = "Invalid Postal Code")]
    public string PostalCode
    {
        get { return _postalCode; }
        set
        {
            if (_postalCode != value)
            {
                _postalCode = value;
            }
        }
    }

How can I implement the proper client and server side validations using this model when a user selects a particular country?

I’m expecting to either do an ajax call to retrieve an updated partial view when the country is changed, or send enough data to the client so I can adjust the client side prompts and validation by modifying the appropriate attributes on the input elements and resetting validation, but how can I get the server side model to validate properly when issuing a Model.IsValid() call?

  • 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-06-05T00:18:39+00:00Added an answer on June 5, 2026 at 12:18 am

    With complex validations, I find it easiest to imeplement IValidatableObject interface

    IEnumerable<ValidationResult> Validate(
        ValidationContext validationContext
    )
    

    Basically something like this

        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            MyAddress model = validationContext.ObjectInstance as MyAddress;
            if (model.Country == "India")
            {
                // validate as india
            }
        }
    

    This seemlessly integrates with default validation system, so you won’t need any additional configurations. But to note, this is only the server side validation.

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

Sidebar

Related Questions

Have an ASP.NET MVC3 app with model validation using FluentValidation . User enters some
I have an ASP.NET MVC3 app that features a form with a nested-table input
I have datetime input in my ASP.NET MVC3 app: <div class=editor-field> @Html.EditorFor(model => model.Article.PublishedDate)
I'm developing a ASP.Net MVC3 app which will have few hundred videos. I want
I am needing to have a dynamic jqgrid on my asp.net mvc3 app. The
I have a controller / action in an ASP.NET MVC3 app. It handles the
I have asp.net mvc app that shows varios events. All events stored in a
I have an ASP.NET MVC 3 app that is using SQL Server CE 4.0
I have an asp.net mvc3 Razor app being developed in VS2010 using Git for
I am writing a little ASP.NET MVC3 app that I am running on Mac

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.