控制器方法无法从重定向中捕获对象路由 [英] Controller method doesn't catch object routes from redirect

查看:71
本文介绍了控制器方法无法从重定向中捕获对象路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm sending data from form to my Test method and the data is there,

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

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?

顺便说一句:向导?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 });
    }
}

推荐答案

将对象作为路由参数传递的问题在于,通过为每个参数调用 ToString 而不是代替 input.FirstName = value& input.SecondName = value 您得到的 input = YourSolution.Controllers.YourController + Model 显然是无效的.就您而言,以下代码足以解决问题

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

这篇关于控制器方法无法从重定向中捕获对象路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆