I wish to send a string to my home controller with ASP.NET MVC. I'm doing this using an Ajax call, however I need to change the view too while sending the string.
The problem is that in order to change page, we need to do it in the Ajax success handler. This results in us calling the controller method once again. This time the date string is null, though, resulting in a null exception. How can we work around this?
our AJAX call:
dayClick: function(date) {
$.ajax({
url: '/Home/Booking',
data: {'selectedDate' : date.format()},
type: "get",
cache: false,
success: function (result) {
window.location.href = "/Home/Booking"
},
error: function (error) {
console.log(error.message);
}
});
}
our controller method:
public IActionResult Booking(string selectedDate)
{
var booking = new Booking();
DateTime selectedDatetime = DateTime.ParseExact(selectedDate, "yyyy-MM-dd", CultureInfo.CurrentCulture);
booking.Date = selectedDatetime;
var viewModel = new BookingsideViewModel
{
Subjects = new[] {"Matematik", "Dansk", "Engelsk"},
Booking = booking
};
return View(viewModel);
}
FormMethod.Get). Of if you like writing extra scripts, then it would be just `dayClick: function(date) { window.location.href = "/Home/Booking?selectedDate=" + date.format(); }