Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7005515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:21:38+00:00 2026-05-27T21:21:38+00:00

I’ve got a view with a JQuery DataTable that loads it’s content via AJAX

  • 0

I’ve got a view with a JQuery DataTable that loads it’s content via AJAX by calling an action method (server-side processing) that returns a nice JSON string with all the data.

What I’m doing is the following:

  1. I create a List that contains the data for the DataTable. Each row in the table is a string array because that’s a requirement for DataTables.
  2. I put all the data for the DataTable in an anonymous object. This data consists of the amount of rows, the data (the List) and some other stuff.
  3. A JSON string is created from this anonymous object so all data is returned in JSON format
  4. Some magic happens and the data is displayed in my DataTable.

The problem is: I can’t output HTML because Razor automatically escapes this. I already tried stuff like Html.Raw, new HtmlString() and stuff like that, but the problem is: I HAVE to put HTML strings in my List collection because DataTables can’t handle any other types as far as I know.

The DataTable in my view doesn’t contain any definition of what columns should be rendered, this is done automatically by just looping through my JSON string (with Javascript or whatever) and displaying it (again, with Javascript).

So the question is: how can I render HTML in my DataTable when using MVC3 server-side processing?

BTW, the code:

<table id="datatable">
    <thead>
        <tr>
            <th>Id</th>
            <th>Name</th>
            <th>Destination address</th>
            <th>Platform</th>
            <th>&nbsp;</th>
            <th>&nbsp;</th>
        </tr>
    </thead>
    <tbody id="tabledata">
    <!-- Content will be rendered here by AJAX -->
    </tbody>
    <tfoot>
        <tr>
            <th>Id</th>
            <th>Name</th>
            <th>Destination address</th>
            <th>Platform</th>
            <th>&nbsp;</th>
            <th>&nbsp;</th>
        </tr>
    </tfoot>
</table>

And the server-side processing:

public JsonResult List()
{
    List<PushApplication> pushApplications = _service.GetData(Request).ToList();

    int totalRecords = _service.GetApplicationCount();

    // Reformat the data.
    List<string[]> aaData = new List<string[]>(pushApplications.Count);
    aaData.AddRange(pushApplications.Select(application => new[]
    {
        application.Id.ToString(),
        application.Name, 
        application.DestinationAddress,
        application.Platform,
        "<a href='/PushApplication/Edit/" + application.Id + "'>Modify</a>",
        "<a href='/PushApplication/Delete/" + application.Id + "'>Delete</a>"
    }));

    // Construct anonymous object for the data table.
    var data = new { sEcho = Request.QueryString["sEcho"], iTotalRecords = totalRecords, iTotalDisplayRecords = totalRecords, aaData = aaData };

    return Json(data, JsonRequestBehavior.AllowGet);
}

I invoke the datatable with the following bit of Javascript:

$(document).ready(function () {
    var oTable = $('#datatable').dataTable({
        "bPaginate": true,
        "bLengthChange": true,
        "bFilter": true,
        "bSort": true,
        "bInfo": true,
        "bAutoWidth": false,
        "bProcessing": true,
        "bServerSide": true,
        "bJQueryUI": true,
        "iDisplayStart": 0,
        "sEcho": 1,
        "sDom": 'T<"clear"><"top"fi>rt<"bottom"pl><"clear">',
        "sAjaxSource": '/PushApplication/List',
        "sPaginationType": "full_numbers",
        "fnServerData": fnDataTablesPipeline,
        "oTableTools": {
            "sSwfPath": "/Plugins/TableTools/swf/copy_cvs_xls_pdf.swf"
        },
        "aoColumns": [
            { sWidth: '10%' },
            { sWidth: '30%' },
            { sWidth: '30%' },
            { sWidth: '30%' }
        ]
    });

    $("#submitForm").click(function () {
        oTable.fnDraw();
    });
});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T21:21:39+00:00Added an answer on May 27, 2026 at 9:21 pm

    I think I misunderstood with my first reply, so apologies if you’ve already read it:

    Are you trying to add HTML into some cells or rows? For example, adding a button that will have extended functionality?

    The best way to do this isn’t in the JSON (although it’s certainly possible with other frameworks) it’s in the DataTables callbacks. If you want to affect individual rows or cells within the row, go for fnRowCallback which I have used to add any number of extra widgets and markup to my cells.

    [update follows]

    From the DataTables website (http://datatables.net/ref#fnrowcallback) here’s how you would do it with a 2D array:

    $(document).ready(function() {
        $('#example').dataTable( {
            "fnRowCallback": function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
                /* Bold the grade for all 'A' grade browsers */
                if ( aData[4] == "A" )
                {
                    $('td:eq(4)', nRow).html( '<b>A</b>' );
                }
                return nRow;
            }
        } );
    } );
    

    Further explanation of the sample: the callback accepts a few different objects, including nRow which is an object instance of the current row, and aData which corresponds to the aaData being sent back by the server side. It’s a simple if statement: if there is the string “A” in the 5th column of data, go to the 5th column inside of nRow object and change its HTML so that instead of “A” it’s a bolded “A”. At the end of the callback, nRow is returned back, in its now-modified state.

    You’re not limited by 2D arrays of course. If you’re using key-value pairs in a 3D object, you might look for something like aData["someLabel"] to modify. And the columns don’t need to correspond; in the example they’re both column 5, but you can do whatever you want. Or if you want to modify the row itself, you can do that, too, instead of looking for a particular td inside of nRow.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a jquery bug and I've been looking for hours now, I can't
I have a French site that I want to parse, but am running into
i got an object with contents of html markup in it, for example: string
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.