Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><em>[UPDATED on 5/24/2012 to make the idea more clear]</em></p> <p>I'm not sure this is the right approach but I think you can extend the concept and can create a more generic / reusable approach.</p> <p>In ASP.NET MVC the validation happens at the binding stage. When you are posting a form to the server the <code>DefaultModelBinder</code> is the one that creates model instances from the request information and add the validation errors to the <code>ModelStateDictionary</code>. </p> <p>In your case, as long as the binding happens with the <code>HomePhone</code> the validations will fire up and <em>I think</em> we can't do much about this by creating <em>custom validation attributes or similar kind</em>. </p> <p>All I'm thinking is not to create model instance at all for <code>HomePhone</code> property when there are no values available in the form <strong>(the areacode, countrycode and number or empty)</strong>, when we control the binding we control the validation, for that, we have to create a <em>custom model binder</em>. </p> <p>In the <em>custom model binder</em> we are checking if the property is <code>HomePhone</code> and if the form contains any values for it's properties and if not we don't bind the property and the validations won't happen for <code>HomePhone</code>. Simply, the value of <code>HomePhone</code> will be null in the <code>UserViewModel</code>.</p> <pre><code> public class CustomModelBinder : DefaultModelBinder { protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor) { if (propertyDescriptor.Name == "HomePhone") { var form = controllerContext.HttpContext.Request.Form; var countryCode = form["HomePhone.CountryCode"]; var areaCode = form["HomePhone.AreaCode"]; var number = form["HomePhone.Number"]; if (string.IsNullOrEmpty(countryCode) &amp;&amp; string.IsNullOrEmpty(areaCode) &amp;&amp; string.IsNullOrEmpty(number)) return; } base.BindProperty(controllerContext, bindingContext, propertyDescriptor); } } </code></pre> <p>Finally you have to register the custom model binder in global.asax.cs.</p> <pre><code> ModelBinders.Binders.Add(typeof(UserViewModel), new CustomModelBinder()); </code></pre> <p>So now of you have an action that takes UserViewModel as parameter,</p> <pre><code> [HttpPost] public Action Post(UserViewModel userViewModel) { } </code></pre> <p>Our custom model binder come into play and of form doesn't post any values for the <strong>areacode, countrycode and number</strong> for <code>HomePhone</code>, there won't be any validation errors and the <code>userViewModel.HomePhone</code> is null. If the form posts atleast any one of the value for those properties then the validation will happen for <code>HomePhone</code> as expected.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload