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 623711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:07:21+00:00 2026-05-13T19:07:21+00:00

Edit: Added bounty because I’m seeking an MVC3 solution (if one exists) other than

  • 0

Edit: Added bounty because I’m seeking an MVC3 solution (if one exists) other than this:

DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;


I have a read only property on my ‘Address’ model 'CityStateZip'.

It’s just a convenient way to get city, state, zip from a US address. It throws an exception if the country is not USA (the caller is supposed to check first).

    public string CityStateZip
    {
        get
        {
            if (IsUSA == false)
            {
                throw new ApplicationException("CityStateZip not valid for international addresses!");
            }

            return (City + ", " + StateCd + " " + ZipOrPostal).Trim().Trim(new char[] {','});
        }
    }

This is part of my model so it gets bound. Prior to ASP.NET MVC2 RC2 this field never caused a problem during databinding. I never even really thought about it – after all it is only read only.

Now though with the January 2010 RC2 release it gives me an error during databinding – becasue the default model binder seems to want to check this value (even though it is read only).

It is the ‘base.OnModelUpdated’ line that causes this error to be triggered.

public class AddressModelBinder : DefaultModelBinder
{
    protected override void OnModelUpdated(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        base.OnModelUpdated(controllerContext, bindingContext);

Last minutes changes to the modelbinder evidently caused this change in behavior – but I’m not quite sure yet what the repurcussions of it are – or whether or not this is a bug? I’m passing this on to the MVC team but curious if anyone else has any suggestions in the meantime how I can prevent this property from binding.

This article is well worth reading about the changes – but doesn’t mention readonly properties at all (not that I would expect it to). The issue (if there is one) may be broader than this situation – I’m just not sure about any repurcussions – if any!

Input Validation vs. Model Validation in ASP.NET MVC


As requested by @haacked here’s the stacktrace :

I get this by simply adding the following line to ANY model and making a post to the corresponding action method. In this instance I added it to my simplest possible model.

 public string Foo { get { throw new Exception("bar"); } }

[TargetInvocationException: Property accessor ‘Foo’ on object ‘Rolling_Razor_MVC.Models.ContactUsModel’ threw the following exception:’bar’]
System.ComponentModel.ReflectPropertyDescriptor.GetValue(Object component) +390
System.Web.Mvc.<>c__DisplayClassb.<GetPropertyValueAccessor>b__a() +18
System.Web.Mvc.ModelMetadata.get_Model() +22
System.Web.Mvc.ModelMetadata.get_RealModelType() +29
System.Web.Mvc.<GetValidatorsImpl>d__0.MoveNext() +38
System.Linq.<SelectManyIterator>d__14`2.MoveNext() +273
System.Web.Mvc.<Validate>d__5.MoveNext() +644
System.Web.Mvc.DefaultModelBinder.OnModelUpdated(ControllerContext controllerContext, ModelBindingContext bindingContext) +92
System.Web.Mvc.DefaultModelBinder.BindComplexElementalModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Object model) +60
System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +1048
System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +280
System.Web.Mvc.Controller.TryUpdateModel(TModel model, String prefix, String[] includeProperties, String[] excludeProperties, IValueProvider valueProvider) +449
System.Web.Mvc.Controller.TryUpdateModel(TModel model) +73

  • 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-13T19:07:21+00:00Added an answer on May 13, 2026 at 7:07 pm

    I believe I’m experiencing a similar issue. I’ve posted the details:

    http://forums.asp.net/t/1523362.aspx


    edit: Response from MVC team (from above URL):

    We investigated this and have concluded that the validation system is behaving as expected. Since model validation involves attempting to run validation over all properties, and since non-nullable value type properties have an implicit [Required] attribute, we’re validating this property and calling its getter in the process. We understand that this is a breaking change from V1 of the product, but it’s necessary to make the new model validation system operate correctly.

    You have a few options to work around this. Any one of these should work:

    • Change the Date property to a method instead of a property; this way it will be ignored by the MVC framework.
    • Change the property type to DateTime? instead of DateTime. This removes the implicit [Required] from this property.
    • Clear the static DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes flag. This removes the implicit [Required] from all non-nullable value type properties application-wide.
      We’re considering adding in V3 of the product an attribute which will signal to us “don’t bind it, don’t validate it, just pretend that this property doesn’t exist.”

    Thanks again for the report!

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

Sidebar

Related Questions

No related questions found

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.