In my controller, I have the following code:
public async Task < ViewResult > Favourites(string ids) {
// API call to fetch book data
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");
}
And in my view, this is the script used:
<script>
function getFavouriteBooks() {
var ids = JSON.parse(localStorage.getItem("bookIds"));
$.ajax({
type: "POST",
url: '/Home/Favourites',
data: { ids: localStorage.getItem('bookIds') },
success: function (response) {
window.location.href = '/Home/Favourites/' + ids ;
}
});
}
</script>
I am unsure about using
window.location.href = '/Home/Favourites/' + ids;
and would like the View called from my controller (return View("Index", matches.ToArray());
) to reload. Any suggestions on how to achieve this?
The parameter is correctly populated with data the first time the controller is accessed, however, it is empty the second time due to the absence of data.
If anyone can provide insights on how to load the View after the ajax call with the ids
parameter, I would greatly appreciate it.