开发者

How to pass errors from one action to another

How can I pass the modelstate errors from oneaction to another in the case of the 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开发者_Go百科");
        }

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


You may consider using TempData to pass an error message.


It is common to return the view on error and redirect when successful.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜