无法在重定向到操作上传递多个参数

本文关键字:参数 操作上 重定向 | 更新日期: 2023-09-27 18:34:08

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

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

所有这些最终所做的只是将用户发送回原始页面,并显示错误消息"对象引用未设置为对象的实例"。

编辑

控制器命中RedirectToAction后,它只是退出控制器而不重定向到 Index 方法,并且视图上出现错误"对象引用未设置为对象的实例"。

无法在重定向到操作上传递多个参数

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

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

将您的操作修改为

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