如何将错误从一个操作传递到另一个操作

时间:2011-02-22 07:33:46

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

在Action Delete?

的情况下,如何将模型状态错误从一个操作传递到另一个操作?

  public ActionResult Index()
    {
        ProjectTestModel model = new ProjectTestModel ();
        return GetProjectView(model);

    }

    public ActionResult GetProjectView(ProjectTestModel model)
    {
        return View("Index", model);
    }

 public ActionResult Delete(int id)
    {
        try
        {
           test.Load(id)
            test.Delete();
            return RedirectToAction("Index");
        }

        catch (Exception e)
        {
            ModelState.AddModelError("Error", e.Message);
            return RedirectToAction("Index");
        }
    }

2 个答案:

答案 0 :(得分:4)

您可以考虑使用TempData传递错误消息。

答案 1 :(得分:2)

通常会在错误时返回视图并在成功时重定向。

public ActionResult Delete(int id)
{
    try
    {
        test.Load(id);
        test.Delete();
        return RedirectToAction("Index");
    }

    catch (Exception e)
    {
        ModelState.AddModelError("Error", e.Message);
        return View("Index");
    }
}
相关问题