Recibir valor nulo al enviar datos al controlador en una llamada ajax

I have a div which contains HTML table and i need to pass that table to controller in MVC3 . I am trying to pass the table using ajax call . While debugging i get my controller to my action declared but the value passed HTML table is coming as null. Do i sending it correctly. what is the problem is sending it. Please refer the below link even this they have faced same problem cómo enviar una tabla html desde la vista al controlador en mvc4

The below is my ajax call code :

$('#frmLogin').submit(function () {

            var sendhtml = $('#divCashResultHolder').html();
           //alert(sendhtml);
            $.ajax({
                url: '@Url.Action("ExportData", "CashCollection")',//action method url which defined in controller
                type: 'POST',
                data: { "htmlTable": sendhtml },
                dataType: 'json',
               contentType: 'application/json; charset=utf-8',
                async: true,
               processData: false,
               success: function(){
   console.log('success!!');
}
            });

El código del controlador

 [HttpPost]
    public ActionResult ExportData(string htmlTable)
    {

            Response.Buffer = true;
            Response.ContentType = "application/vnd.ms-excel";
            Response.AddHeader("Content-disposition", "attachment;filename=CashCollection_" + DateTime.Now.Ticks + ".xls");

        return View("CashCollection");
    }

In the controller i get 'string htmlTable' as null value. I tried setting it to HTMLstring type no luck. Also, i tried sending data: JSON.stringify(sendhtml ) no luck for this too.

preguntado el 27 de noviembre de 13 a las 07:11

intente configurar processData a true -

Ok thank u i am getting value in controller but only if div contains string value then, in the controller parameter it shows value. if the div contains table then it does not come to controller itself -

2 Respuestas

Try passing the data using JSON.stringify

 data: JSON.stringify({"htmlTable": sendhtml});

respondido 27 nov., 13:07

No its not hitting to controller. It hits to controller with this data: { "htmlTable": sendhtml }, and this gets hit only if the div as string value. if it div contains table it does not hit. In that div it contains HTML table. if the html table is not there then it display string "No informaton" - user1003054

check what happens with $('#divCashResultHolder').val() - nitin varpe

Thanks for ur help i used javascript excel download method to download the excel file. - user1003054

The problem here is while data is coming to controller. It is validating. By default they do not allow to send html data to control.

To allow we could use [ValidateInput (falso)]

Then your controller should look like this:

[HttpPost]
[ValidateInput(false)]
    public ActionResult ExportData(string htmlTable)
    {

            Response.Buffer = true;
            Response.ContentType = "application/vnd.ms-excel";
            Response.AddHeader("Content-disposition", "attachment;filename=CashCollection_" + DateTime.Now.Ticks + ".xls");

        return View("CashCollection");
    }

If this is not working. Then its may be your ajax method problem. Then try to send your data as FormData. Then your ajax method should like this:

$('#frmLogin').submit(function () {

            var sendhtml = $('#divCashResultHolder').html();
           //alert(sendhtml);
        var fd = new FormData();
        fd.append("htmlTable", sendhtml );

            $.ajax({
                url: '@Url.Action("ExportData", "CashCollection")',//action method url which defined in controller
                type: 'POST',
                data: fd,
                enctype: 'application/form-data',
                processData: false,
                contentType: false,
                success: function(){
                        console.log('success!!');
               }
            });

Respondido 07 Jul 17, 07:07

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.