title: ASP.Net MVC 3 - Client side unobtrusive validation for Editor Templates
body: <p>I am new to ASP.Net MVC 3, facing some issues while trying to implementing client side unobtrusive validation for a editor template I have created for showing date in a custom way.</p>
  
  <p><strong>UI</strong><br>
  I need to show date in a <em>three texbox UI</em> format as</p>
  
  <p><img src="https://i.stack.imgur.com/QCOUa.jpg" alt="enter image description here"></p>
  
  <p><br/>
  I have put up a <em>EditorTemplate</em> for displaying the date in three parts as </p>
  
  <pre><code>@model DateTime?
  
  &lt;table class="datetime"&gt;
  &lt;tr&gt;
      &lt;td&gt;@Html.TextBox("Day", (Model.HasValue ? Model.Value.ToString("dd") : string.Empty)) &lt;/td&gt;
      &lt;td class="separator"&gt;/&lt;/td&gt;
      &lt;td&gt;@Html.TextBox("Month", (Model.HasValue ? Model.Value.ToString("MM") : string.Empty))&lt;/td&gt;
      &lt;td class="separator"&gt;/&lt;/td&gt;
      &lt;td&gt;@Html.TextBox("Year", (Model.HasValue ? Model.Value.ToString("yyyy") : string.Empty))&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
      &lt;td class="label"&gt;dd&lt;/td&gt;
      &lt;td/&gt;
      &lt;td class="label"&gt;mm&lt;/td&gt;
      &lt;td/&gt;
      &lt;td class="label"&gt;yyyy&lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p><strong>Model</strong>
  <br/>
  I have to bind a Date of Birth field which is a property in a <em>subobject</em> of my model to this property, in this structure</p>
  
  <pre><code>MyModel
      --&gt; MySubModel
            --&gt; DateOfBirth
  
  public class MySubModel
  {
      ...
  
      [DataType(DataType.Date)]
      [Display(Name = "Date of birth")]
      [DateTimeClientValidation()]
      public DateTime DateofBirth { get; set; }
  
      ...
  }
  </code></pre>
  
  <p><strong>Clientside Validation</strong>
  <br/></p>
  
  <p>I have put up a custom validation attribute which implements IClientValidatable as</p>
  
  <pre><code>public class DateTimeClientValidationAttribute : ValidationAttribute, IClientValidatable
  {
      ...
  
      public IEnumerable&lt;ModelClientValidationRule&gt; GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
      {
          List&lt;ModelClientValidationRule&gt; clientRules = new List&lt;ModelClientValidationRule&gt;();
  
          //Combined date should be valid
          ModelClientValidationRule validDateRule = new ModelClientValidationRule
          {
              ErrorMessage = "Please enter a valid date.",
              ValidationType = "validdate"
          };
          validDateRule.ValidationParameters.Add("dayelement", metadata.PropertyName + ".Day");
          validDateRule.ValidationParameters.Add("monthelement", metadata.PropertyName + ".Month");
          validDateRule.ValidationParameters.Add("yearelement", metadata.PropertyName + ".Year");
          clientRules.Add(validDateRule);
  
          return clientRules;
      }
      ...
  }
  </code></pre>
  
  <p>I am trying to emit the element names of Day, Month &amp; Year textboxes here to client side validation elements, so that I will write a client side jquery validation method and adapter later which would consume those elements and do the validation at the client side.</p>
  
  <p><strong>View</strong>
  <br/>
  Now, to use this editor template, I put in <em>View</em> the following lines</p>
  
  <pre><code>@model MyModel
  ...
  &lt;tr&gt;
      &lt;td class="editor-label"&gt;
          @Html.LabelFor(m =&gt; m.MySubModel.DateofBirth)
      &lt;/td&gt;
      &lt;td class="editor-field"&gt;
          @Html.EditorFor(m =&gt; m.MySubModel.DateofBirth)
          @Html.ValidationMessageFor(m =&gt; m.MySubModel.DateofBirth)
      &lt;/td&gt;
  &lt;/tr&gt;
  ...
  </code></pre>
  
  <p><em>Added all relevant jquery validation files in the view as references</em></p>
  
  <p><strong>Questions</strong>
  <br/></p>
  
  <ol>
  <li>This is not emitting out the unobtrusive javascript validation attributes in the html, though I have implemented <em>IClientValidatable</em>. 
  For testing purpose when I put the same attribute (<em>DateTimeClientValidation</em>) on another property in the model which was not using this editor template, then it emitted out those validation attributes, it is not emitting it out only for this editor template. Where could have I gone wrong ?</li>
  <li>Regarding Validation Message span for the editor template, is it right that I put it in View only or should I put it directly in the editor template (<em>@Html.ValidationMessageFor(m => m.MySubModel.DateofBirth)</em>)</li>
  <li>In this example, am I right in the design, I have put in <em>DateTimeClientValidationAttribute</em>, which actually is an attribute I put on model, but this component knows a bit about UI (since it is trying to emit out the Day, Month &amp; Year elements name to the client), this makes Model know a bit about View, am I breaking any design principles here ?</li>
  <li>In <em>DateTimeClientValidationAttribute</em>, I am trying to emit out the day, month &amp; year elements names to the client, so that the client script can do validations on it. But since the model property <em>DateofBirth</em> is in a subobject, the actual element name in the script is <em>MySubObject.DateOfBirth</em>, which makes the Day textbox name to be <em>MySubObject.DateofBirth.Day</em>, how can I find that fully qualified model name in the <em>GetClientValidationRules</em> method, so that I can emit out the name to the client ?</li>
  </ol>
  
  <p>Thanks for being patient to read out all this, and for the answers</p>