Reputation: 929
I have a requirement, where I need to pass the same instance of a ViewModel
through to multiple actions in a controller. I am aware you can do this using RedirectToAction
like below. The ViewModel
has a property named ServiceTimes
(which is a list). This has one item inside it, however when entering my OnlineBookingStaff
action, the ServiceTimes
property in here is null. Why is this?
[HttpPost]
public async Task<IActionResult> OnlineBookingServices(OnlineBookingViewModel viewModel)
{
try
{
return RedirectToAction(nameof(OnlineBookingStaff), viewModel);
}
}
The Action looks like this:
public async Task<IActionResult> OnlineBookingStaff(OnlineBookingViewModel viewModel)
{
try
{
return View(viewModel);
}
catch (Exception ex)
{
await ExceptionAsync("Problem displaying Staff, Please try again later.", ex, false, true);
return View();
}
}
Upvotes: 1
Views: 879
Reputation: 32222
RedirectToAction
sends an HTTP 302 response and causes a client side redirect, which will end up in a GET to the url, losing your model.
You can instead just call it directly:
[HttpPost]
public async Task<IActionResult> OnlineBookingServices(OnlineBookingViewModel viewModel)
{
return await OnlineBookingStaff(viewModel);
}
Upvotes: 2