-1

i have a problem, i work with MVC and i am trying to save HTML code of view in a DB field. I have on JS side of my MVC Solution the following code:

        var data = { id_perizia: $("#id_perizia").val(), pinSessione: $("#pin_sessione").val(), saveRegistration: flgSave, markup: document.documentElement.innerHTML }

    $.ajax({
        type: "POST",
        url: WebPerizie.baseUrl + "Registra/stopRecording",
        data: data,
        success: function (resp) {                  
            window.location.href = WebPerizie.baseUrl + "PT_PERIZIE/Edit/" + $("#id_perizia").val();
        },
        failure: function () {
            //console.log("Failed:::" + resp);
        },
        error: function (e) {
            //console.log("Status code Error==="+e.status);
        },
    });

and my controller function parameter is:

public ActionResult stopRecording(decimal id_perizia, string pinSessione, bool saveRegistration, byte[] markup)

from the debugger i enter in the Controller, but i don't understand because the markup variable is always null. When i use in the browser console the command:

$("html").html();

or

document.documentElement.innerHTML

i always get the HTML code of the page in the markup variable, but in the controller i have null. I have the sensation i wrong to pass the variable markup to the controller and the markup variable has the right value in the JS code. What do you think about it? What do i wrong? Can i get the HTML View code directly from the controller? If i can, is it a better solution?

Marchin
  • 23
  • 1
  • 6

3 Answers3

1

There are many possibility.

First of all check you are getting html or not in alert() and use string for html in your C# code (Remove byte). And also use validation false

Like below code

[ValidateInput(false)] 
public ActionResult stopRecording(decimal id_perizia, string pinSessione, bool saveRegistration, string markup)

Hope this will help you out

Bharat
  • 2,441
  • 3
  • 24
  • 36
0

You should use Html Form tag and pass like this

var data= $( "form" ).serialize();
    $.ajax({
        type: "POST",
        url: WebPerizie.baseUrl + "Registra/stopRecording",
        data: data,
        success: function (resp) {                  
            window.location.href = WebPerizie.baseUrl + "PT_PERIZIE/Edit/" + $("#id_perizia").val();
        },
        failure: function () {
            //console.log("Failed:::" + resp);
        },
        error: function (e) {
            //console.log("Status code Error==="+e.status);
        },
    });
Naveen
  • 1,441
  • 2
  • 16
  • 41
0

You can pass a string variable to controller and convert string to HtmlObject on controller side. this is better option.

  1. JS side:

            var HtmlCode = '<div></div>'
    
            var dataArgs = '{' +
               '"code": "' + HtmlCode +
               '"}';
    
            var request = $.ajax({
    
                url: '@Url.Action("MethodName", "controllerName")',
                type: "POST",
                headers: headers,
                contentType: "application/json; charset=utf-8",
                data: dataArgs,  
                dataType: "json"
            });
    
            request.done(function (msg) {
    
                //success
            });
    
            request.fail(function (jqXHR, textStatus) {
                alert('Error submitting request.');
            });
    
  2. c# side : string to HtmlDocument. String to HtmlDocument

Community
  • 1
  • 1
Ajay Kumar Oad
  • 560
  • 5
  • 15