Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to create a reusable way to use the excellent jQuery datatables 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.

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.

Now, with the Razor view engine this is easy as pie, you could write something like this:

@foreach (var person in people)
{
    <tr>
        <td>
            <span>@person.Name</span>
            <img src="@person.ImgUrl" title="@person.Name"/>
        </td>
    </tr>
}

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)

And there's the problem. I'm hesitant to create partial views for every table for every HTML column.

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.

So, to reiterate:

  1. I want to show a table of entities using the jQuery datatable plugin with AJAX server side processing.
  2. I want to write my display logic in my view, using the Razor view engine
  3. I want to handle the jQuery datatable AJAX requests in my controller
  4. I want to somehow ** access the display logic per column** written in my view from within my controller to construct the JSON response. This is because some columns can have custom HTML, load partials, etc.

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.

Thank you in advance for your help.

share|improve this question

1 Answer

I'm not sure if I entirely follow the issue but I think you can solve this very easily. Instead of trying your controllers to reference the views which doesn't work, you should have jQuery pass the parameters that you need for the controller back in the Ajax request. Then the controller can use those parameters to send proper JSON response.

I think you are getting stuck on number 2:

I want to write my display logic in my view, using the Razor view engine

This is difficult to do with datatables.net because the display logic is in javascript. You could use a JS template plugin (like mustache) and write that in your view. Then in your datatables.net js you can plug the JSON into the templates.

share|improve this answer
Yes, I know how I can pass ajax parameters to my controller. The problem here is actually a bit of a mismatch between javascript and C#. For example, when I configure a column that contains a datetime, I want to display it with a dateformat, but when I sort over it it should just use the datetime value, not the string representation. That mustache templating engine looks interesting though, do you have any examples to point me in the right direction? – Moeri Apr 12 at 13:31
I had this issue. datatables passes back the column that needs to be sorted to the server when you do an AJAX call. In your controller you need a switch statement to convert the proper column into datetime so it will sort properly. – Rafi Apr 12 at 13:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.