title: How to implement server side processing in .Net MVC with jQuery Datatables and HTML rendering from within the Controller
body: <p>I want to create a reusable way to use the excellent <a href="http://www.datatables.net/" rel="nofollow">jQuery datatables</a> plugin with .Net MVC for all my views that contain tables. Now, I already worked out how server side processing with this plugin works and how I need to write my controller logic to provide the appropriate response.</p>
  
  <p>The problem starts with rendering html though. Say for example that I need to render a picture next every person's name in a 'people table'. However, when I type in the search box, it should still correctly search by the names of those people.</p>
  
  <p>Now, with the Razor view engine this is easy as pie, you could write something like this:</p>
  
  <pre><code>@foreach (var person in people)
  {
      &lt;tr&gt;
          &lt;td&gt;
              &lt;span&gt;@person.Name&lt;/span&gt;
              &lt;img src="@person.ImgUrl" title="@person.Name"/&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
  }
  </code></pre>
  
  <p>The problem is that this needs to be rendered to JSON on the server side. You would have a Controller that handles the jQuery datatables AJAX requests and returns a valid JSON object containing everything the datatable needs. However, you can't access snippets from your view in your controller. The only option I see here is to create partial views for every column that needs to render some specific HTML. (For common use cases, like action buttons, I could write HTML helpers)</p>
  
  <p>And there's the problem. I'm hesitant to create partial views for every table for every HTML column. </p>
  
  <p>The issue here is that the controller needs to somehow access the display logic to create JSON, while the display logic is typically written in the view. 
  My options are limited to using partial views and rendering those to a string in the controller, or storing my display logic (which are lambda expressions) in some sort of server session or cache and fetching it from the controller. The big downside to this last approach is that sessions or cache seem too volatile to depend on. </p>
  
  <p>So, to reiterate:</p>
  
  <ol>
  <li>I want to show a table of entities using the jQuery datatable plugin with AJAX <strong>server side processing</strong>.</li>
  <li>I want to write my <strong>display logic in my view</strong>, using the Razor view engine</li>
  <li>I want to <strong>handle</strong> the jQuery datatable AJAX <strong>requests in my controller</strong></li>
  <li>I want to somehow ** access the display logic per column** written in my view <strong>from within my controller</strong> to construct the JSON response. This is because some columns can have custom HTML, load partials, etc.</li>
  </ol>
  
  <p>I'm really quite stuck on this, if there are similar questions on Stackoverflow about this please point me to them because I haven't found them. </p>
  
  <p>Thank you in advance for your help.</p>