【问题标题】:Unable to pass multiple parameters on RedirectToAction无法在 RedirectToAction 上传递多个参数
【发布时间】:2023-04-07 22:38:01
【问题描述】:

我的每个控制器方法都需要重定向回索引页面并将它们发送回控制器的模型对象发送回控制器。但是,在一种情况下,我需要与模型对象一起发送一条错误消息。下面是 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."} );

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

编辑

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

【问题讨论】:

  • 我认为问题是“search = updateZip”。请删除此参数,然后重试。告诉我们结果。
  • @Satpal: ZipCodeIndex 是一个视图模型对象。
  • @KleberBernardo:我需要能够将 updateZip 视图模型对象传递给 Index 方法,这是它的签名的一部分。另外,以防万一,我将其作为重定向参数删除,结果是相同的(没有重定向到 Index 方法,出现空对象引用错误消息)。

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


【解决方案1】:

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

【讨论】:

  • 控制器没有重定向到 Index 方法,并且在视图上我收到一条错误消息说Object reference not set to an instance of an object
猜你喜欢
  • 1970-01-01
  • 2011-09-25
  • 1970-01-01
  • 2014-11-05
  • 2019-06-10
  • 1970-01-01
  • 2019-10-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多