title: asp.net mvc associate resource files for display templates based on location of ascx file
body: <p>I'm trying to minimise the amount of repeated code in my project. A lot of the views we have summarise data from our viewmodels. the viewmodels abstract some quite modular data. When displaying this stuff the structure of the modular stuff is always the same, the text however changes depending on which view is hosting the display template. So... </p>
  
  <p>in section1 i might have:</p>
  
  <pre><code>public class Section1ReportViewModel{
      [UiHint("Person")]
      [Display(ResourceType = typeof(Resources.Section1), Name="ReportedPerson")]
      public Person  ReportedPerson {get; set; } 
  
      [UiHint("Address")]
      [Display(ResourceType = typeof(Resources.Section1), Name="HomeAddress")]
      public Address HomeAddress {get; set; }
  
      [UiHint("Person")]
      [Display(ResourceType = typeof(Resources.Section1), Name="AssociatedPerson")]
      public Person  AssociatedPerson {get; set; } 
  }
  </code></pre>
  
  <p>the report view in Views/Section1/index.aspx would might then have:</p>
  
  <pre><code>@Model Section1ReportViewModel
  
  @Html.DisplayFor(x=&gt;x.ReportedPerson)
  @Html.DisplayFor(x=&gt;x.HomeAddress)
  @Html.DisplayFor(x=&gt;x.AssociatedPerson)
  </code></pre>
  
  <p>Mvc can look in the Shared/DisplayTemplates folder to retrieve the display templates using the UiHint attribute.</p>
  
  <p>Now, so that i don't go re-creating all the views and stuff, in section 2 i would have:</p>
  
  <pre><code>public class Section2ReportViewModel{
      [UiHint("Person")]
      [Display(ResourceType = typeof(Resources.Section2), Name="ReportedPerson")]
      public Person  ReportedPerson {get; set; } 
  
      [UiHint("Address")]
      [Display(ResourceType = typeof(Resources.Section2), Name="HomeAddress")]
      public Address HomeAddress {get; set; }
  
      [UiHint("Person")]
      [Display(ResourceType = typeof(Resources.Section2), Name="AssociatedPerson")]
      public Person  AssociatedPerson {get; set; } 
  }
  </code></pre>
  
  <p>and the view in Views/Section2/index.aspx</p>
  
  <pre><code>@Model Section1ReportViewModel
  
  @Html.DisplayFor(x=&gt;x.ReportedPerson)
  @Html.DisplayFor(x=&gt;x.HomeAddress)
  @Html.DisplayFor(x=&gt;x.AssociatedPerson)
  </code></pre>
  
  <p>And as an example of one of the display templates (Person.ascx):</p>
  
  <pre><code>@Model Person 
  &lt;h1&gt;@Html.Resource("Title")&lt;/h1&gt;
  &lt;dl&gt;
      @Html.DetailLineFor(x=&gt;x.FullName)
      @Html.DetailLineFor(x=&gt;x.DateOfBirth)
  &lt;/dl&gt;
  </code></pre>
  
  <p>Now... here's the puzzle. I want to keep these shared views in the Shared/DisplayTemplates folder. But I don't want to keep the resource files for them in the Shared/DisplayTemplates/App_LocalResources folder. I want to keep the resource file in Views/Section2/DisplayTemplates/App_LocalResources/Person.ascx.resx</p>
  
  <p>The reason is so that the text for the h1 tag will be specific to Section2 and when the same template is used in section 1 the text from Views/Section1/DisplayTemplates/App_LocalResources/Person.ascx.resx will be used.</p>
  
  <p>Is there any way to get this working? I had it in my head that the search paths for this stuff would be able to handle it, but apparently not :(</p>