c# - 无法在 RedirectToAction 上传递多个参数

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

我的每个 Controller 方法都需要重定向回索引页面,并将它们发送回 Controller 的模型对象发送回 Controller 。但是,在一个实例中,我需要将错误消息与模型对象一起发送。下面是 Index 方法的签名:

    public ViewResult Index(ZipCodeIndex search, string unspecifiedAction = "")

因为我只需要来自一种方法的错误消息,所以我将此参数设为可选。这是我尝试从单独的操作重定向到索引的方式:

        //the parameter 'updateZip' is a model object of type ZipCodeIndex
        return RedirectToAction("Index", new { search = updateZip, unspecifiedAction = "Error: Action could not be determined. IT has been notified and will respond shortly."} );

所有这一切最终都会将用户发送回原始页面,并显示错误消息“对象引用未设置到对象的实例。”

编辑

在 Controller 点击 RedirectToAction 后,它只是退出 Controller 而不重定向到 Index 方法,并且 View 上出现错误“对象引用未设置为对象的实例”。

最佳答案

你不能在 RedirectToAction 中传递类对象,所以移除 search = updateZip 参数。

如果你需要的话。您可以将其作为替代方案传递到 TempData

修改你的 Action 为

public ViewResult Index(string unspecifiedAction = ""){
      var search = (ZipCodeIndex)TempData["ZipCodeIndexData"];
      //rest of code
}

重定向

TempData["ZipCodeIndexData"] = updateZip;
return RedirectToAction("Index", new { unspecifiedAction = "Error: Action could not be determined. IT has been notified and will respond shortly."} );

关于c# - 无法在 RedirectToAction 上传递多个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19188334/

相关文章:

c# - Regex.Escape 的目的是什么?

.net - 在 IIS 中运行时 WCF 服务无法连接到 Web 服务

asp.net-mvc - 在 Controller 中创建 HtmlHelper 实例

asp.net mvc session 示例

c# - 蕴涵运算反射(reflect)了什么?

c# - Azure 移动服务 - Entity Framework 代码首先删除并创建数据库不起作用

c# - 有没有办法实现 ZeroMQ 全双工 channel ?

c# - 在 WebBrowser 控件中设置 TextArea 的值 (C#/.NET)

c# - 将 C++ 结构转换为 C#

asp.net-mvc - 如何转义以使用页面名称,例如字符串 "Index"