How to transfer errors from one action to another

How can I pass modelstate errors from oneaction to another in the case of an 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");
        }
    }
+3
source share
2 answers

Normally return a view of the error and redirect on success.

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");
    }
}
+2
source

You can use TempData to send an error message.

+4
source

Source: https://habr.com/ru/post/1793797/


All Articles