asp.net MVC JavaScript call to controller

0

I'm currently working on an ASP.Net MVC project. I have a JavaScript function which takes an XML string as input. I would like to send this to the controller.

I have done so using an AJAX request, but in the controller the string is null.

View:

        function save() {
            var xml = scheduler.toXML();
            alert(xml);

            var url = '@Url.Action("Save", "Home")'

            $.ajax({
                url: url,
                Type: "POST",
                dataType: 'json',
                async: false,
                data: xml,
                contentType: 'application/json; charset=utf-8',
                success: function (data) { alert("OK");},
                error: function (jqXHR, exception) {
                alert('Error message.');
            }
            });

Controller:

 public ActionResult Save(string xml)
    {
        Console.WriteLine(xml);

        W6ViewModel viewModel = new W6ViewModel();
        viewModel.engineers = db.W6ENGINEERS.ToList();
        viewModel.tasks = db.W6TASKS.ToList();
        viewModel.skills = db.W6TASKS_REQUIRED_SKILLS1.ToList();

        var engList = new List<object>();
        foreach (var engineer in viewModel.engineers)
        {
            engList.Add(new { key = engineer.ID, label = engineer.Name });
        }
        ViewBag.engineers = engList;

        return View("Index", viewModel);
    }

var xml = scheduler.toXML()

alert(xml):

enter image description here

Error Code (Sorry, wall of text):

[HttpRequestValidationException (0x80004005): A potentially dangerous Request.QueryString value was detected from the client (xmlString=&quot;&lt;data&gt;&lt;event&gt;
c#
javascript
asp.net-mvc
asp.net-mvc-4
controller
asked on Stack Overflow Jul 3, 2014 by Stephen Sugumar • edited Feb 4, 2016 by gariepy

2 Answers

4

Name your parameter like this:

function save() {
        var xml = scheduler.toXML();
        alert(xml);

        var url = '@Url.Action("Save", "Home")';

        $.ajax({
            url: url,
            Type: "POST",
            dataType: 'json',
            async: false,
            data: { xml: xml},
            contentType: 'application/json; charset=utf-8',
            success: function (data) { alert("OK");},
            error: function (jqXHR, exception) {
            alert('Error message.');
        }
        });

Also put this tag above you controller action:

[ValidateInput(false)]
answered on Stack Overflow Jul 3, 2014 by Paweł Reszka • edited Jul 3, 2014 by Paweł Reszka
0

See the following ajax call:

  $.ajax({
            url: '@Url.Content("~/myaccount/CheckDuplicateEmailAddress")',
            data: { "emailAddress": email },
            async: false,
            type: "post",
            success: success,
            error: error
        });

And controller action is below. you need to send param as this:

data: { "emailAddress": email }

Remember case sensitivity and double quotes:

public bool CheckDuplicateEmailAddress(string emailAddress)
    {
    }
answered on Stack Overflow Jul 3, 2014 by Learner • edited Jan 31, 2020 by riki481

User contributions licensed under CC BY-SA 3.0