我正在将数据从表单发送到我的 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 });
关于c# - Controller 方法不会从重定向中捕获对象路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54531257/