Ok, so my question is two pronged. I am trying to retrieve specific string values from selected rows into a JQuery array:
<script>
var finalCSG=[];
$('.checkbox').change(function () {
var csg = [];
var checkedRows = $('.checkbox:checked');
$.each(checkedRows, function (index, item)
{
var row = $(this).closest('tr');
csg.push(row.find('.connote').text());
})
finalCsg = csg;
});
</script>
My first question: The contents of array 'finalCsg' are supposed to be simple alphanumeric strings, but they contain lots of leading and trailing spaces (when I used console.log to check them out). How do I resolve this?
Now, the second and more pressing issue. I try to pass this array to my MVC controller (with a button click), with the following code:
var url = '@Url.Action("Assign", "TransactionHistory")';
document.getElementById("AssignButton").onclick = function () { myFunction() };
function myFunction() {
$('#myDiv').load(url, { csgList: finalCsg })
}
'TransactionHistory' is my controller name. Even here, the variable csgList contains the proper array elements (with those spaces I described earlier, though, when I check with console.log). But my controller only gets a null string. The controller is defined like this:
[HttpPost]
public ActionResult Assign(List<string> csgList)
{
return Content("Hi");
}
I initially tried using string[] csgList as the parameter, and then tried List but it always gets a null value (or empty string I guess) from JQuery (The return statement is just to test, does not really mean much now). Some posts mention the use of a 'traditional' keyword, but I am not primarily a front end developer (I am covering for someone at work, with strict deadlines) so I couldn't use that to proper effect.
Any inputs would be greatly beneficial.
{ [0].csgList: someValue, [1].csgList: anotherValue, .... }(i,e. indexed) or you need to stringify the data and setcontentType: 'json'(or you can usetraditional: trueoption. Its easier to do this using$.ajax().string[]orIEnumerable<string>or List<string>` are all fine. But you cannot passcsgListas it is. It needs to be as I noted in the first comment - e.g. stringified usingJSON.stringify()and set thecontentTypeoption