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

标签 c# asp.net-core asp.net-core-mvc

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

相关文章:

asp.net - Asp.Core 中的模型绑定(bind)与继承

c# - 如何使用 ASP.NET Core 获取当前路由名称?

asp.net - 如何在 ASP NET MVC 6 中更新模型?

asp.net-core-mvc - 在类库中访问 appsettings.json

c# - 生成所有递增的数字

c# - 使用 SemaphoreSlim 限制 TPL 数据流的问题

docker - Docker中带有ASP.NET Core 2.1的"It was not possible to find any compatible framework version"

c# - 如何存储对基于传递给方法的表达式创建的泛型类型的引用?

c# - 在使用 Mono 运行的 C# 应用程序中更改当前 Linux 用户?

javascript - 如何创建新的 .NET Core SPA Angular 2 项目