我的每个 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/