title: RazorEngine - How to use a complex model view?
body: <p>Anyone have experience working with a complex model and RazorEngine?</p>
  
  <p>Working on generating HTML using <a href="https://github.com/Antaris/RazorEngine" rel="nofollow">RazorEngine</a> version 3.7.3, but running into issues with the complex model view we have.  It seems like we should be able to use the templates to get RazorEngine to discover the SubSample below, but have not discovered the proper way to tell RazorEngine about the associated cshtml file.</p>
  
  <p>In the example below we are looking to use a shared template for the SubSample class using the SubSample.cshtml file.  As can be seen from the results, the class namespace (ReportSample.SubSample) is displayed rather than an HTML row of data.</p>
  
  <p>We have tried implementing an ITemplateManager, but Resolve() is never called with a key asking for the SubSample.  Also tried AddTemplate() on the service, but still no joy.</p>
  
  <p>Here is a simplified example model to illustrate the issue:</p>
  
  <pre><code>namespace ReportSample
  {
      public class SubSample
      {
          public string Name { get; set; }
          public string Value { get; set; }
      }
  
      public class SampleModel
      {
          public SubSample SubSample { get; set; }
      }
  }
  </code></pre>
  
  <p><strong>SampleModel.cshtml</strong></p>
  
  <pre><code>@using ReportSample
  @*@model ReportSample.SampleModel*@
  &lt;table style="width: 7.5in" align="center"&gt;
      &lt;tr&gt;
          &lt;td align="center"&gt;
              &lt;h1&gt;
                  Sample Report
              &lt;/h1&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;
              &lt;table style="width: 100%"&gt;
                  &lt;tr&gt;
                      &lt;td colspan="2"&gt;
                          &lt;b&gt;Name:&lt;/b&gt; @Model.SubSample.Name
                      &lt;/td&gt;
                      &lt;td colspan="2"&gt;
                          &lt;b&gt;Value:&lt;/b&gt; @Model.SubSample.Value
                      &lt;/td&gt;
                  &lt;/tr&gt;
              &lt;/table&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td align="center"&gt;
              &lt;h1&gt;
                  Sub-sample Data
              &lt;/h1&gt;
          &lt;/td&gt;
          &lt;td&gt;
              &lt;table style="width: 100%"&gt;
                  @Model.SubSample
              &lt;/table&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p><strong>SubSample.cshtml</strong></p>
  
  <pre><code>@model ReportSample.SubSample
  @using FSWebportal.Infrastructure.Mvc;
  &lt;tr class="observation-row"&gt;
      &lt;td class="observation-label"&gt;
          @model.Name
      &lt;/td&gt;
      &lt;td class="observation-view"&gt;
          @model.Value
      &lt;/td&gt;
  &lt;/tr&gt;
  </code></pre>
  
  <p><strong>Basic RazorEngine calls:</strong></p>
  
  <pre><code>private void html_Click(object sender, EventArgs e)
  {
      var gen = new RazorEngineGenerator();
      var cshtmlTemplate = File.ReadAllText("Sample.cshtml");
  
      var sample = new SampleModel() { SubSample = new SubSample() { Name = "name", Value = "value" } };
  
      var html = gen.GenerateHtml(sample, cshtmlTemplate);
  }
  
  public string GenerateHtml&lt;T&gt;(T model, string cshtmlTemplate)
  {
      var config = new TemplateServiceConfiguration();
      using (var service = RazorEngineService.Create(config))
      {
          return service.RunCompile(cshtmlTemplate, "", typeof(T), model);
      }
  }
  </code></pre>
  
  <p><strong>Sample HTML Output:</strong>
      
          
              
                  <h1>
                      Sample Report
                  </h1>
              
          
          
              
                  
                      
                          
                              <b>Name:</b> name
                          
                          
                              <b>Value:</b> value
                          
                      
                  
              
          
          
              
                  <h1>
                      Sub-sample Data
                  </h1>
              
              
                  
                      ReportSample.SubSample
                  
              
          
      </p>