gpt4 book ai didi

c# - Controller 方法不会从重定向中捕获对象路由

转载 作者:太空宇宙 更新时间:2023-11-03 12:06:38 25 4
gpt4 key购买 nike

我正在将数据从表单发送到我的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com