I am trying to redict to another MVC View from a an Angular 1.6 $http.post. Basically I want to go to another View (Checkout) to display the options user has selected from Details page. The trouble I am having is that if I go directly to Checkout I get a NullReferenceException, so I tried this:
Angular:
model.submitDetails = function () {
// Commented out code results in NullReferenceExeption - not binding the data
//var data = {
// car: model.car, colour: model.colour, features: model.features, cover: model.cover
//}
//window.location = 'http://localhost:50592/Cars/Checkout?vm=' + data;
$http.post('http://localhost:50592/Cars/DetailsSubmit',
{ car: model.car, colour: model.colour, features: model.features, cover: model.cover })
.then(function (response) {
window.location.href = response;
}, function (err) {
model.error = err.data;
console.log(err.data);
});
};
The above $http.post results in a 404 error. If I don't include the .then, e.g. comment it out, I can see in the Network tab in F12 dev tools that the $http.post is returning the html directly rather than rendering the Checkout View page.
My MVC Controller Code:
[HttpPost]
public ActionResult DetailsSubmit(CheckoutViewModel vm)
{
TempData["car"] = vm.Car;
vm.StartDate = DateTime.Now;
vm.EndDate = DateTime.Now.AddDays(7);
return RedirectToAction("Checkout", vm);
}
public ActionResult Checkout(CheckoutViewModel vm)
{
vm.Car = (Car)TempData["car"];
return View(vm);
}
To how can I redirect successfully to the Checkout page from the submitDetails() Angular function? Any help getting this working would be greatly appreciated.