title: How to validate my model in a custom model binder?
body: <p>I asked about an issue I have with comma delimited numeric values <a href="https://stackoverflow.com/questions/23098059/comma-delimited-numeric-value-fails-validation-in-asp-net-mvc-4">here</a>.</p>
  
  <p>Given some of the replies, I attempted to try to implement my own model binder as follows:</p>
  
  <pre class="lang-cs prettyprint-override"><code>namespace MvcApplication1.Core
  {
      public class PropertyModelBinder : DefaultModelBinder
      {
          public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
          {
              object objectModel = new object();
  
              if (bindingContext.ModelType == typeof(PropertyModel))
              {
                  HttpRequestBase request = controllerContext.HttpContext.Request;
                  string price = request.Form.Get("Price").Replace(",", string.Empty);
  
                  ModelBindingContext newBindingContext = new ModelBindingContext()
                  {
                      ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(
                          () =&gt; new PropertyModel() 
                          {
                              Price = Convert.ToInt32(price)
                          },
                          typeof(PropertyModel)       
                      ),
                      ModelState = bindingContext.ModelState,
                      ValueProvider = bindingContext.ValueProvider
                  };
  
                  // call the default model binder this new binding context
                  return base.BindModel(controllerContext, newBindingContext);
              }
              else
              {
                  return base.BindModel(controllerContext, bindingContext);
              }
          }
  
          //protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)
          //{
          //    //return base.CreateModel(controllerContext, bindingContext, modelType);
          //    PropertyModel model = new PropertyModel();
  
          //    if (modelType == typeof(PropertyModel))
          //    {
          //        model = (PropertyModel)base.CreateModel(controllerContext, bindingContext, modelType);
          //        HttpRequestBase request = controllerContext.HttpContext.Request;
          //        string price = request.Form.Get("Price").Replace(",", string.Empty);
          //        model.Price = Convert.ToInt32(price);
          //    }
  
          //    return model;
          //}
      }
  }
  </code></pre>
  
  <p>And updated my controller class as this:</p>
  
  <pre class="lang-cs prettyprint-override"><code>namespace MvcApplication1.Controllers
  {
      public class PropertyController : Controller
      {
          public ActionResult Edit()
          {
              PropertyModel model = new PropertyModel
              {
                  AgentName = "John Doe",
                  BuildingStyle = "Colonial",
                  BuiltYear = 1978,
                  Price = 650000,
                  Id = 1
              };
  
              return View(model);
          }
  
          [HttpPost]
          public ActionResult Edit([ModelBinder(typeof(PropertyModelBinder))] PropertyModel model)
          {
              if (ModelState.IsValid)
              {
                  //Save property info.              
              }
  
              return View(model);
          }
  
          public ActionResult About()
          {
              ViewBag.Message = "Your app description page.";
  
              return View();
          }
  
          public ActionResult Contact()
          {
              ViewBag.Message = "Your contact page.";
  
              return View();
          }
      }
  }
  </code></pre>
  
  <p>Now, if I enter the price with commas, my custom model binder will remove the commas, that's what I want, but validation still fails.  So, question is: <b>How to do custom validation in my custom model binder such that the captured price value with commas can be avoided</b>? In other words, I suspect that I need to do more in my custom model binder, but don't know how and what.  Thanks.<img src="https://i.stack.imgur.com/z9O0I.png" alt="Open the screen shot in a new tab for a better view."></p>
  
  <p><strong>Update:</strong></p>
  
  <p>So, I tried @mare's solution at <a href="https://stackoverflow.com/a/2592430/97109">https://stackoverflow.com/a/2592430/97109</a> and updated my model binder as follows:</p>
  
  <pre class="lang-cs prettyprint-override"><code>namespace MvcApplication1.Core
  {
      public class PropertyModelBinder : DefaultModelBinder
      {
          public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
          {
              object objectModel = new object();
  
              if (bindingContext.ModelType == typeof(PropertyModel))
              {
                  HttpRequestBase request = controllerContext.HttpContext.Request;
                  string price = request.Form.Get("Price").Replace(",", string.Empty);
  
                  ModelBindingContext newBindingContext = new ModelBindingContext()
                  {
                      ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(
                          () =&gt; new PropertyModel() 
                          {
                              Price = Convert.ToInt32(price)
                          },
                          typeof(PropertyModel)    
                      ),
                      ModelState = bindingContext.ModelState,
                      ValueProvider = bindingContext.ValueProvider
                  };
  
                  // call the default model binder this new binding context
                  object o = base.BindModel(controllerContext, newBindingContext);
                  newBindingContext.ModelState.Remove("Price");
                  newBindingContext.ModelState.Add("Price", new ModelState());
                  newBindingContext.ModelState.SetModelValue("Price", new ValueProviderResult(price, price, null));
                  return o;
              }
              else
              {
                  return base.BindModel(controllerContext, bindingContext);
              }
          }
  
          //protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)
          //{
          //    //return base.CreateModel(controllerContext, bindingContext, modelType);
          //    PropertyModel model = new PropertyModel();
  
          //    if (modelType == typeof(PropertyModel))
          //    {
          //        model = (PropertyModel)base.CreateModel(controllerContext, bindingContext, modelType);
          //        HttpRequestBase request = controllerContext.HttpContext.Request;
          //        string price = request.Form.Get("Price").Replace(",", string.Empty);
          //        model.Price = Convert.ToInt32(price);
          //    }
  
          //    return model;
          //}
      }
  }
  </code></pre>
  
  <p>It sorta works, but if I enter 0 for price, the model comes back as valid, which is wrong because I have a Range annotation which says that the minimum price is 1.  At my wit's end.</p>
  
  <p><strong>Update:</strong></p>
  
  <p>In order to test out a custom model binder with composite types. I've created the following view model classes:</p>
  
  <pre class="lang-cs prettyprint-override"><code>using System.ComponentModel.DataAnnotations;
  
  namespace MvcApplication1.Models
  {
      public class PropertyRegistrationViewModel
      {
          public PropertyRegistrationViewModel()
          {
  
          }
  
          public Property Property { get; set; }
          public Agent Agent { get; set; }
      }
  
      public class Property
      {
          public int HouseNumber { get; set; }
          public string Street { get; set; }
          public string City { get; set; }
          public string State { get; set; }
          public string Zip { get; set; }
  
          [Required(ErrorMessage="You must enter the price.")]
          [Range(1000, 10000000, ErrorMessage="Bad price.")]
          public int Price { get; set; }
      }
  
      public class Agent
      {
          public string FirstName { get; set; }
          public string LastName { get; set; }
          [Required(ErrorMessage="You must enter your annual sales.")]
          [Range(10000, 5000000, ErrorMessage="Bad range.")]
          public int AnnualSales { get; set; }
  
          public Address Address { get; set; }
      }
  
      public class Address
      {
          public string Line1 { get; set; }
          public string Line2 { get; set; }
      }
  }
  </code></pre>
  
  <p>And here is the controller:</p>
  
  <pre class="lang-cs prettyprint-override"><code>using MvcApplication1.Core;
  using MvcApplication1.Models;
  using System.Web.Mvc;
  
  namespace MvcApplication1.Controllers {
      public class RegistrationController : Controller
      {
          public ActionResult Index() {
              PropertyRegistrationViewModel viewModel = new PropertyRegistrationViewModel();
              return View(viewModel);
          }
  
          [HttpPost]
          public ActionResult Index([ModelBinder(typeof(PropertyRegistrationModelBinder))]PropertyRegistrationViewModel viewModel)
          {
              if (ModelState.IsValid)
              {
                  //save registration.
              }
  
              return View(viewModel);
          }
      }
  }
  </code></pre>
  
  <p>Here is the custom model binder implementation:</p>
  
  <pre class="lang-cs prettyprint-override"><code>using MvcApplication1.Models;
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.Mvc;
  
  namespace MvcApplication1.Core
  {
      public class PropertyRegistrationModelBinder : DefaultModelBinder
      {
          protected override object GetPropertyValue(
              ControllerContext controllerContext,
              ModelBindingContext bindingContext,
              System.ComponentModel.PropertyDescriptor propertyDescriptor,
              IModelBinder propertyBinder)
          {
              if (propertyDescriptor.ComponentType == typeof(PropertyRegistrationViewModel))
              {
                  if (propertyDescriptor.Name == "Property")
                  {  
                      var price = bindingContext.ValueProvider.GetValue("Property.Price").AttemptedValue.Replace(",", string.Empty);
                      var property = new Property();
  
                      // Question 1: Price is the only property I want to modify. Is there any way 
                      // such that I don't have to manually populate the rest of the properties like so?
                      property.Price = string.IsNullOrWhiteSpace(price)? 0: Convert.ToInt32(price);
                      property.HouseNumber = Convert.ToInt32(bindingContext.ValueProvider.GetValue("Property.HouseNumber").AttemptedValue);
                      property.Street = bindingContext.ValueProvider.GetValue("Property.Street").AttemptedValue;
                      property.City = bindingContext.ValueProvider.GetValue("Property.City").AttemptedValue;
                      property.State = bindingContext.ValueProvider.GetValue("Property.State").AttemptedValue;
                      property.Zip = bindingContext.ValueProvider.GetValue("Property.Zip").AttemptedValue;
  
                      // I had thought that when this property object returns, our annotation of the Price property
                      // will be honored by the model binder, but it doesn't validate it accordingly.
                      return property;
                  }
  
                  if (propertyDescriptor.Name == "Agent")
                  {
                      var sales = bindingContext.ValueProvider.GetValue("Agent.AnnualSales").AttemptedValue.Replace(",", string.Empty);
                      var agent = new Agent();
  
                      // Question 2: AnnualSales is the only property I need to process before validation,
                      // Is there any way I can avoid tediously populating the rest of the properties?
                      agent.AnnualSales = string.IsNullOrWhiteSpace(sales)? 0:  Convert.ToInt32(sales);
                      agent.FirstName = bindingContext.ValueProvider.GetValue("Agent.FirstName").AttemptedValue;
                      agent.LastName = bindingContext.ValueProvider.GetValue("Agent.LastName").AttemptedValue;
  
                      var address = new Address();
                      address.Line1 = bindingContext.ValueProvider.GetValue("Agent.Address.Line1").AttemptedValue + " ROC";
                      address.Line2 = bindingContext.ValueProvider.GetValue("Agent.Address.Line2").AttemptedValue + " MD";
                      agent.Address = address;
  
                      // I had thought that when this agent object returns, our annotation of the AnnualSales property
                      // will be honored by the model binder, but it doesn't validate it accordingly.
                      return agent;
                  }
              }
              return base.GetPropertyValue(controllerContext, bindingContext, propertyDescriptor, propertyBinder);
          }
  
          protected override void OnModelUpdated(ControllerContext controllerContext, ModelBindingContext bindingContext)
          {
              var model = bindingContext.Model as PropertyRegistrationViewModel;
              //In order to validate our model, it seems that we will have to manually validate it here. 
              base.OnModelUpdated(controllerContext, bindingContext);
          }
      }
  }
  </code></pre>
  
  <p>And here is the Razor view:</p>
  
  <pre class="lang-cs prettyprint-override"><code>@model MvcApplication1.Models.PropertyRegistrationViewModel
  @{
      ViewBag.Title = "Property Registration";
  }
  
  &lt;h2&gt;Property Registration&lt;/h2&gt;
  &lt;p&gt;Enter your property and agent information below.&lt;/p&gt;
  
  @using (Html.BeginForm("Index", "Registration"))
  {
      @Html.ValidationSummary();    
      &lt;h4&gt;Property Info&lt;/h4&gt;
      &lt;text&gt;House Number&lt;/text&gt; @Html.TextBoxFor(m =&gt; m.Property.HouseNumber)&lt;br /&gt;
      &lt;text&gt;Street&lt;/text&gt; @Html.TextBoxFor(m =&gt; m.Property.Street)&lt;br /&gt;
      &lt;text&gt;City&lt;/text&gt; @Html.TextBoxFor(m =&gt; m.Property.City)&lt;br /&gt;
      &lt;text&gt;State&lt;/text&gt; @Html.TextBoxFor(m =&gt; m.Property.State)&lt;br /&gt;
      &lt;text&gt;Zip&lt;/text&gt; @Html.TextBoxFor(m =&gt; m.Property.Zip)&lt;br /&gt;
      &lt;text&gt;Price&lt;/text&gt; @Html.TextBoxFor(m =&gt; m.Property.Price)&lt;br /&gt; 
      &lt;h4&gt;Agent Info&lt;/h4&gt;
      &lt;text&gt;First Name&lt;/text&gt; @Html.TextBoxFor(m =&gt; m.Agent.FirstName)&lt;br /&gt;
      &lt;text&gt;Last Name&lt;/text&gt; @Html.TextBoxFor(m =&gt; m.Agent.LastName)&lt;br /&gt;
      &lt;text&gt;Annual Sales&lt;/text&gt; @Html.TextBoxFor(m =&gt; m.Agent.AnnualSales)&lt;br /&gt;
      &lt;text&gt;Agent Address L1&lt;/text&gt;@Html.TextBoxFor(m =&gt; m.Agent.Address.Line1)&lt;br /&gt;
      &lt;text&gt;Agent Address L2&lt;/text&gt;@Html.TextBoxFor(m =&gt; m.Agent.Address.Line2)&lt;br /&gt;
      &lt;input type="submit" value="Submit" name="submit" /&gt;
  }
  </code></pre>
  
  <p>And here is the global.asax file where I wire up the custom model binder. BTW, it seems this step isn't needed, coz I notice it still works without this step.</p>
  
  <pre class="lang-cs prettyprint-override"><code>using MvcApplication1.Core;
  using MvcApplication1.Models;
  using System.Web.Http;
  using System.Web.Mvc;
  using System.Web.Optimization;
  using System.Web.Routing;
  
  namespace MvcApplication1 {
      // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
      // visit http://go.microsoft.com/?LinkId=9394801
  
      public class MvcApplication : System.Web.HttpApplication {
          protected void Application_Start() {
              AreaRegistration.RegisterAllAreas();
  
              WebApiConfig.Register(GlobalConfiguration.Configuration);
              FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
              RouteConfig.RegisterRoutes(RouteTable.Routes);
              BundleConfig.RegisterBundles(BundleTable.Bundles);
              AuthConfig.RegisterAuth();
              ModelBinders.Binders.Add(typeof(PropertyRegistrationViewModel), new PropertyRegistrationModelBinder());
          }
      }
  }
  </code></pre>
  
  <p>Maybe I am doing something wrong or not enough. I've noticed the following problems:</p>
  
  <ol>
  <li>
  Although I only need to modify the Price value of the property object, it seems I have to tediously populate all the other properties in the model binder. I have to do the same for the AnnualSales property of the agent property.  Is there anyway this can be avoided in the model binder?
  </li>
  <li>
  I had thought that the default BindModel method will honor our annotation of our objects' properties and validate them accordingly after it calls GetPropertyValue, but it doesn't. If I enter some value way out of range for Price of the Property object or the AnnualSales of the Agent object, the model comes back as valid.  In other words, the Range annotations are ignored.  I know I can validate them by overriding OnModelUpdated in the custom model binder, but that's too much work, and plus, I have the annotations in place, why doesn't the default implementation of the model binder honor them just because I am overriding part of it?
  </li>
  </ol>
  
  <p>@dotnetstep: Could you throw some insights into this? Thank you.</p>