Take HTML from page and Send to Controller

Asked

Viewed 65 times

1

Hello,

I’m trying to take all the html of the page from the tag and send it to my controller, but I’m not getting it, follow code used.

    function exportarExcel() {
    var url = '@Url.Action("ExportExcel")';
    var html = $("body").html();
    $.ajax({
        url: url,
        data: {
            Html: html,
        },
    });
}

Function in the Controller:

    public void ExportExcel(string Html)
    {
        Classes.Export.ToExcelHtml(Response, Html.ToString());

    }
  • It’s like this action of yours ?

  • I edited the post with the function

1 answer

4


ASP.NET ignored your request for security reasons (XSS, Cross-Site Scripting). Before the method in the controller, use the attribute ValidateInput:

[ValidateInput(false)]
public void ExportExcel(string Html)
{
   Classes.Export.ToExcelHtml(Response, Html.ToString());       
}

Your Javascript:

  function exportarExcel() {
    var url = '@Url.Action("ExportExcel")';
    var html = $("body").html();
    $.ajax({
        url: url,
        type: 'POST',
        data: {
            Html: html,
        },
    });
}

Sources:
ASP.NET Request Validation
jquery - Sending HTML to Controller using Ajax POST - Stack Overflow

Browser other questions tagged

You are not signed in. Login or sign up in order to post.