控制器方法不从重定向中捕获对象路由



我正在将数据从表单发送到我的Test方法,数据就在那里,

如果发生了一些错误,那么我正在将我的ModelInput映射到Model,然后我正在执行重定向到通过对象路由发送的数据MyView。出于某种原因MyView参数input为空,即使Test中的input具有正确的值

知道为什么重定向后我的数据(输入参数(会丢失吗?

顺便说一句:吉德?ID 已正确发送

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 });
    }
}

对象作为路由参数传递的问题在于,生成的 url 是通过为每个参数调用 ToString 来构建的,而不是input.FirstName=value&input.SecondName=value你得到的input=YourSolution.Controllers.YourController+Model这显然是无效的。在您的情况下,以下代码足以解决问题

return RedirectToAction(nameof(MyView), new { id, input.FirstName, input.SecondName });