问题
I'm sending data from form to my Test
method and the data is there,
If some error has occured, then I'm mapping my ModelInput
to Model
and then I'm performing an redirect to MyView
with data sent via Object routes. For some reason in MyView
param input
is null, even if input
in Test
had correct values
Any idea why after redirect my data (input param) is being lost?
BTW: Guid? Id is sent correctly
public IActionResult MyView(Guid? id, Model input = null)
{
// after redirect input is empty
(...)
}
__
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Test(Guid id, ModelInput user_input)
{
(...)
if (error)
{
var input = new Model
{
FirstName = user_input.FirstName,
SecondName = user_input.SecondName
}
return RedirectToAction(nameof(MyView), new { id, input });
}
}
回答1:
The problem with passing an object as a route parameter is that the resulting url is built by calling ToString
for every parameter and instead of input.FirstName=value&input.SecondName=value
you got input=YourSolution.Controllers.YourController+Model
which is obviously invalid. In your case the following code will be sufficient to fix the problem
return RedirectToAction(nameof(MyView), new { id, input.FirstName, input.SecondName });
来源:https://stackoverflow.com/questions/54531257/controller-method-doesnt-catch-object-routes-from-redirect