title: ASP.NET MVC + FluentValidation + PartialView > no client-side validation
body: <p>I use FluentValidation to validate my ASP.NET MVC 5.1 viewmodels (using the default unobtrusive client-side validation). I register/create my validators via Ninject (no attributes on the viewmodels):</p>
  
  <pre><code>FluentValidationModelValidatorProvider.Configure(x =&gt; x.ValidatorFactory = new NinjectValidatorFactory(_kernel));
  AssemblyScanner.FindValidatorsInAssembly(Assembly.GetExecutingAssembly()).ForEach(match =&gt; _kernel.Bind(match.InterfaceType).To(match.ValidatorType));
  </code></pre>
  
  <p>I have a partial view that is used often throughout the application. This partial view has its own viewmodel. The "main" viewmodel (the one for the entire view) just inherits from this "partial" viewmodel.</p>
  
  <pre><code>public class IndexViewModel : PersonalInfoViewModel { ... }
  </code></pre>
  
  <p>The same pattern is used on the validators:</p>
  
  <pre><code>public class IndexValidator : PersonalInfoValidator { ... }
  </code></pre>
  
  <p>This works fine and the server-side validation works as well as the client-side validation on the "main" view. But the client-side validation on the partial view is not triggered (no <code>data-val-*</code> attributes on input fields there).</p>
  
  <p>My "main" view <code>Index.cshtml</code></p>
  
  <pre><code>@model IndexViewModel
  @Html.TextBoxFor(x =&gt; x.SomeProperty) // client-side validation works fine
  
  @Html.Partial("PersonalInfo") // client-side validation in the partial view does not work (see below)
  </code></pre>
  
  <p>My partial view <code>PersonalInfo.cshtml</code>:</p>
  
  <pre><code>@model PersonalInfoViewModel
  @Html.TextBoxFor(x =&gt; x.FirstName) // client-side validation does not work
  </code></pre>
  
  <p>I noticed that it works when I change the model for the partial view to the "main" viewmodel:</p>
  
  <pre><code>@model IndexViewModel
  @Html.TextBoxFor(x =&gt; x.FirstName) // client-side validation works
  </code></pre>
  
  <p>So I guess somewhere when building the client-side validation the correct validator does not get picked up and no <code>data-val-*</code> attributes are injected into the HTML. But I can't changed the viewmodel on the partial view, because it's used on several different pages with different "main" views (all inherit from PersonalInfoViewModel though).</p>
  
  <p>Any ideas on how to get my client-side validation working in this case?</p>
  
  <p><strong>Update</strong></p>
  
  <p>After a few hours of digging through the ASP.NET WebStack source, I found that the problem seems to occur in the <code>TextBoxFor</code> method. There the <code>ModelMetaData</code> is created from the model - and in there somewhere it starts to use <code>IndexViewModel</code> instead of <code>PersonalInfoViewModel</code> to get the metadata for client-side validation:</p>
  
  <pre><code>ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
  Html.GetUnobtrusiveValidationAttributes("FirstName", metadata); // no client-side validation attributes found
  </code></pre>
  
  <p>But this generates the correct client-side validation attributes if I don't specify the metadata explicitly:</p>
  
  <pre><code>Html.GetUnobtrusiveValidationAttributes("FirstName");
  </code></pre>
  
  <p>Unfortunately, I have not found a way to use this knowledge for my advantage so far.</p>