1

I want to post with AJAX a string array with some data to my controller. It's just plain text, and my controller is always receiving a null parameter. I understand i shouldn't stringify since i don't use a model or viewmodel.

I searched other questions but most refer to forms and use viewmodel properties.

Here is my code:

Controller

[HttpPost]
public ActionResult FirstAjax(string[] listValues)
{
    //TODO
    return Json("Reached the controller", JsonRequestBehavior.AllowGet);
}

I added that JSON return to check if I was actually hitting the controller and I receive the message on my view.

AJAX POST

var listValues = [];
listElements.each(function (index, element) {
    listValues.push(element.innerText);
});

var serviceURL = '/Products/FirstAjax';

$.ajax({
    type: "POST",
    url: serviceURL,
    data: listValues,
    success: successFunc,
    error: errorFunc
});

function successFunc(data, status) {
    alert(data);
}

function errorFunc() {
    alert('error');
}

Since my list is sortable with drag & drop, the listValues is filled with the text value of the <li> items in the order when the button is clicked.

View

<div class="demo">

  <ul id="sortable">
    <li class="ui-state-default">Cat</li>
    <li class="ui-state-default">Dog</li>
    <li class="ui-state-default">Tiger</li>
  </ul>
  <button type="button" onclick="display_array();">Ajax Post!</button>

</div><!-- End demo -->
3
  • 1
    In your ajax, for your data, try this - "data : {listValues: listValues}". The first parameter in the curly brackets being the parameter in controller and second one being your javascript array. Commented Aug 20, 2018 at 16:30
  • Thanks @Daniaal, that worked! Commented Aug 20, 2018 at 16:39
  • dam i should have gave it as an answer! Oh well glad it worked amigo. Commented Aug 21, 2018 at 8:04

3 Answers 3

0

Write your Ajax POST method as follows:

$(document).ready(function(){

    var listValues = [];
    listElements.each(function (index, element) {
          listValues.push(element.innerText);
    });

    var serviceURL = '/Products/FirstAjax';

    $.ajax({
       type: "POST",
       url: serviceURL,
       data: {listValues: listValues},
       contentType: 'application/json'
       success: successFunc,
       error: errorFunc
    });

    function successFunc(data, status) {
       alert(data);
    }

    function errorFunc() {
       alert('error');
    }
});

Hope this will solve your problem.

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

Comments

0

Change your ajax options to:

$.ajax({
    ...
    data: {listValues: listValues},
    ...
});

the reason is: server is expecting a posted object/query string that's same naming with parameters. the data is converted to listValues=...&otherParams=... in query string. if you post an array without specifying parameter name, JQuery cannot map them correctly

Comments

0

You need contentType as application/json and use JSON.stringify to convert JavaScript object to JSON string.

$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: serviceURL,
    data: JSON.stringify({ listValues: listValues}),
    success: successFunc,
    error: errorFunc
});

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.