1

I have problem sending XML data fron Ajax to ASP.NET MVC. Data is not sent. Ajax code:

`function SendXmlToServer(ServerXml) {
       $.ajax({ url: "/Home/XmlData",
           type: "POST",
            processData: false, 
           data: { ResXml: ServerXml }, dataType: "xml",
           success: function () {
               alert("Successful");
               return false;
           }
       })
   }`

ASP.NET MVC code:

[HttpPost]
    public ActionResult XmlData(string ResXml) 
    {   
        return null;
    }

Why ResXml variable has null?

0

1 Answer 1

0

The default model binding doesn't work with processData set to false. If ServerXml is a string of XML, removing this should make it work:

function SendXmlToServer(ServerXml) {
   $.ajax({ url: "/Home/XmlData",
       type: "POST",
       data: { ResXml: ServerXml }, dataType: "xml",
       success: function () {
           alert("Successful");
           return false;
       }
   });
}

You'll also have to add the ValidateInput attribute to your action method, because normally "HTML markup" isn't allowed:

[HttpPost]
[ValidateInput(false)]
public ActionResult XmlData(string ResXml) 
{   
    return null;
}

Alternatively, you could use custom model binding to seamlessly deserialize the XML, as explained in this blog post.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.