Controller method doesn't catch object routes from redirect

风流意气都作罢 提交于 2021-01-27 18:12:19

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!