I'm attempting to send an array of strings from my local storage (key value) to an MVC controller. Here's the code I have so far:
Inside the cshtml View file:
<script>
function getFavouriteBooks() {
var ids = JSON.parse(localStorage.getItem("bookIds"));
$.ajax({
type: "POST",
traditional: true,
url: '@Url.Action("Favourites", "Home")',
data: { ids: ids },
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (result) {
alert(result.Result);
}
}});
}
</script>
<button onClick="getFavouriteBooks()">Display Favourites</button>
The Controller section in my code:
public async Task < ViewResult > Favourites(string ids) {
if (data != null)
{
var bookList = JsonConvert.DeserializeObject < Book[] > (data);
foreach(var book in bookList) {
var matches = new List < bookList > ();
if (bookList.All(book => ids.Contains(book.Id))) {
matches.Add(book);
}
return View("Index", matches.ToArray());
}
}
return View("Index");
}
Although the controller action is being successfully called on button click, the 'ids' parameter continues to be null despite containing values in the console. Can anyone point me in the right direction?