title: Updating ModelState with model object
body: <p>The problem: How to update ModelState in posting+validation scenario.</p>
  
  <p>I've got a simple form:</p>
  
  <pre><code>&lt;%= Html.ValidationSummary() %&gt;
  &lt;% using(Html.BeginForm())%&gt;
  &lt;%{ %&gt;
      &lt;%=Html.TextBox("m.Value") %&gt;
      &lt;input type="submit" /&gt;
  &lt;%} %&gt;
  </code></pre>
  
  <p>When user submits I want to validate input and in some circumstances I want to fix the error for user, letting him know that he made an error that is already fixed:</p>
  
  <pre><code>[AcceptVerbs(HttpVerbs.Post)]
  public ActionResult Index(M m)
  {
      if (m.Value != "a")
      {
          ModelState.AddModelError("m.Value", "should be \"a\"");
          m.Value = "a";
          return View(m);
      }
      return View("About");            
  }
  </code></pre>
  
  <p>Well the problem is, MVC will simply ignore the model passed to the view and will re-render whatever the user typed -- and not my value ("a").
  This happens, because the TextBox renderer checkes if there is a ModelState and if it's not null - ModelState's value is used. That value is of course the one user typed before posting.</p>
  
  <p>Since I can't change the behaviour of TextBox renderer the only solution I found would be to update the ModelState by myself. The quick'n'dirty way is to (ab)use the DefaultModelBinder and override the method that assigns the values from forms to model by simply changing the assignment direction ;). Using DefaultModelBinder I don't have to parse the ids.
  The following code (based on original implementation of DefaultModelBinder) is my solution to this:</p>
  
  <pre><code>/// &lt;summary&gt;
      /// Updates ModelState using values from &lt;paramref name="order"/&gt;
      /// &lt;/summary&gt;
      /// &lt;param name="order"&gt;Source&lt;/param&gt;
      /// &lt;param name="prefix"&gt;Prefix used by Binder. Argument name in Action (if not explicitly specified).&lt;/param&gt;
      protected void UpdateModelState(object model, string prefix)
      {
          new ReversedBinder().BindModel(this.ControllerContext,
              new ModelBindingContext()
              {
                  Model = model,
                  ModelName = prefix,
                  ModelState = ModelState,
                  ModelType = model.GetType(),
                  ValueProvider = ValueProvider
              });
      }
  
      private class ReversedBinder : DefaultModelBinder
      {
          protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor propertyDescriptor)
          {
              string prefix = CreateSubPropertyName(bindingContext.ModelName, propertyDescriptor.Name);
              object val = typeof(Controller)
                  .Assembly.GetType("System.Web.Mvc.DictionaryHelpers")
                  .GetMethod("DoesAnyKeyHavePrefix")
                  .MakeGenericMethod(typeof(ValueProviderResult))
                  .Invoke(null, new object[] { bindingContext.ValueProvider, prefix });
              bool res = (bool)val;
              if (res)
              {
  
                  IModelBinder binder = new ReversedBinder();//this.Binders.GetBinder(propertyDescriptor.PropertyType);
                  object obj2 = propertyDescriptor.GetValue(bindingContext.Model);
  
                  ModelBindingContext context2 = new ModelBindingContext();
                  context2.Model = obj2;
                  context2.ModelName = prefix;
                  context2.ModelState = bindingContext.ModelState;
                  context2.ModelType = propertyDescriptor.PropertyType;
                  context2.ValueProvider = bindingContext.ValueProvider;
                  ModelBindingContext context = context2;
                  object obj3 = binder.BindModel(controllerContext, context);
  
                  if (bindingContext.ModelState.Keys.Contains&lt;string&gt;(prefix))
                  {
                      var prefixKey = bindingContext.ModelState.Keys.First&lt;string&gt;(x =&gt; x == prefix);
                      bindingContext.ModelState[prefixKey].Value
                                      = new ValueProviderResult(obj2, obj2.ToString(),
                                                                  bindingContext.ModelState[prefixKey].Value.Culture);
                  }
              }
          }
      }
  </code></pre>
  
  <p>So the question remains: am I doing something extremely uncommon or am I missing something? If the former, then how could I implement such functionality in a better way (using existing MVC infrastructure)?</p>