【问题标题】:Passing model from one action to another asp.net mvc将模型从一个动作传递到另一个 asp.net mvc
【发布时间】:2014-06-19 21:04:41
【问题描述】:

我有一个名为 ErrorController 的控制器,其操作为 Index

我正在捕获来自其他控制器的异常,即HomeController,并将异常存储在名为ExceptionDetails的模型中,如下所示。

public class ExceptionDetail
  {
    public string message { get; set; }
    public string details { get; set; }
  }

一旦捕获到异常,我会将其存储在上面显示的模型中,并希望将其发送到控制器ErrorController 的操作Index

我该怎么做呢

我尝试了以下方法,但没有成功。

catch (System.Exception exception)
  {
    exceptionDetail.details = exception.ToString();
    exceptionDetail.details = exception.Message.ToString();
    return RedirectToAction("Index", "Error", new { exception = exceptionDetail });
  }

Index 操作如下所示,我也有索引的强类型视图。

 public ActionResult Index(ExceptionDetail exceptionDetail)
  {
    return View(exceptionDetail);
  }

提前感谢您的帮助。

【问题讨论】:

  • 您能否详细说明“没用”
  • 我找到了解决方案@Jonesy 将发布答案

标签: c# asp.net-mvc asp.net-mvc-4 model action


【解决方案1】:

你的方法签名是:

public ActionResult Index(ExceptionDetail exceptionDetail)

所以你的 RedirectToAction 应该定义一个参数“exceptionDetail”:

return RedirectToAction("Index", "Error", exceptionDetail);

【讨论】:

    【解决方案2】:
    return RedirectToAction("Index", "Error", exceptionDetail);
    

    通过这种方式,我可以在控制器 ErrorController 的动作 Index 处使用模型属性

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-19
      • 1970-01-01
      • 2021-12-13
      • 1970-01-01
      • 2017-09-18
      • 1970-01-01
      • 2018-04-21
      • 2013-01-12
      相关资源
      最近更新 更多