首页 文章

无法在RedirectToAction上传递多个参数

提问于
浏览
2

我的每个控制器方法都需要重定向回Index页面并将它们发布的模型对象发送回控制器 . 但是,在一个实例中,我需要发送错误消息以及模型对象 . 以下是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."} );

所有这些结果都是将用户发送回原始页面,并显示错误消息“对象引用未设置为对象的实例” .

EDIT

在控制器命中 RedirectToAction 之后,它只是退出控制器而不重定向到Index方法,并且视图上出现错误"Object refrerence not set to an instance of an object" .

1 回答

  • 3

    您无法在 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."} );
    

相关问题