title: Why does ASP.NET MVC care about my read only properties during databinding?
body: <p><strong>Edit: Added bounty because I'm seeking an MVC3 solution (if one exists) other than this:</strong></p>
  
  <p>DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;</p>
  
  <hr>
  
  <p>I have a read only property on my 'Address' model <code>'CityStateZip'</code>.</p>
  
  <p>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).</p>
  
  <pre><code>    public string CityStateZip
      {
          get
          {
              if (IsUSA == false)
              {
                  throw new ApplicationException("CityStateZip not valid for international addresses!");
              }
  
              return (City + ", " + StateCd + " " + ZipOrPostal).Trim().Trim(new char[] {','});
          }
      }
  </code></pre>
  
  <p>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.</p>
  
  <p>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).</p>
  
  <p>It is the 'base.OnModelUpdated' line that causes this error to be triggered.</p>
  
  <pre><code>public class AddressModelBinder : DefaultModelBinder
  {
      protected override void OnModelUpdated(ControllerContext controllerContext, ModelBindingContext bindingContext)
      {
          base.OnModelUpdated(controllerContext, bindingContext);
  </code></pre>
  
  <p>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.</p>
  
  <p>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!</p>
  
  <p><a href="http://bradwilson.typepad.com/blog/2010/01/input-validation-vs-model-validation-in-aspnet-mvc.html" rel="noreferrer">Input Validation vs. Model Validation in ASP.NET MVC</a></p>
  
  <hr>
  
  <p>As requested by @haacked here's the stacktrace :</p>
  
  <p>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.</p>
  
  <pre><code> public string Foo { get { throw new Exception("bar"); } }
  </code></pre>
  
  <blockquote>
    <p><strong>[TargetInvocationException: Property accessor 'Foo' on object 'Rolling_Razor_MVC.Models.ContactUsModel' threw the following exception:'bar']</strong>
       System.ComponentModel.ReflectPropertyDescriptor.GetValue(Object component) +390
       System.Web.Mvc.&lt;&gt;c__DisplayClassb.&lt;GetPropertyValueAccessor&gt;b__a() +18
       System.Web.Mvc.ModelMetadata.get_Model() +22
       System.Web.Mvc.ModelMetadata.get_RealModelType() +29
       System.Web.Mvc.&lt;GetValidatorsImpl&gt;d__0.MoveNext() +38
       System.Linq.&lt;SelectManyIterator&gt;d__14`2.MoveNext() +273
       System.Web.Mvc.&lt;Validate&gt;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</p>
  </blockquote>