jQuery / MVC : Passing long big html string / normal string to Controller as a parameter


I learn today that if your parameter value string is too long , you can’t use GET method with .ajax of jquery. In my case, i had to pass long html string generated in View to Controller. I was using .ajax method without any type : POST declaration, so when my control reach to controller, i used to get null value in parameter in C#.

IF you are using the GET method, you are limited to a maximum of 2,048 characters, minus the number of characters in the actual path.

However, the POST method is not limited by the size of the URL for submitting name/value pairs. These pairs are transferred in the request body and not in the URL.

So here is what I did :

var htmlText = “<html><dl>” + ($(“#dispositionDiv”).html()) + “</dl></html>”;
alert($.toJSON(htmlText));
try {
$.ajax({
url: baseUrl + “/_GetDispositionPDF”,
contentType: “application/json; charset=utf-8”,
type: “POST”,
data: “{strData : ‘” + $.toJSON(htmlText) + “‘ }”,
success: function (result) {
alert(result.status);
}
});
} catch (e) {
alert(e);
}

***********************************************

This article code helped me :

$(document).ready(function() {
    $("#Search").click(function(event) {

        var searchString = $("#TraderSearch").val();            
      $.ajax({
            type: 'POST',
            url: '/Build/SearchTrader',
            data: "{strData : '" + searchString + "' }",
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            success: function(ResultList) {
                var contents = "";
                var count = 0;
                $(ResultList).each(function() {
                contents = contents + '<tr><td>' + ResultList[count].Name + '</td><td>' + ResultList[count].Value +
                     '</td><td><a><img src="../../html/images/Edit.gif" width="14" height="14" alt="edit" /></a></td></tr>';
                    count = count + 1;
                });
                $("#SerachResultList").append(contents);
                alert("{strData : '" + searchString + "' }");
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                alert("Error: " + textStatus + "\n" + errorThrown);

            }
        });

    });
});
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s