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 -->