0

let's look at the code below. When the compiler goes out from the HttpPost action I want to redisplay the blank View with the msg object. How to do that? I don't want to use jQuery to clear fields because I have many DropDownLists being stored in the ViewData dictionary (in the HttpGet action).

I've read the topic RedirectToAction with parameter (the Kurt's answer) but I don't want to modify my URL.

The code below redisplay all inserted data into the View.

    [HttpGet]
    public ActionResult Add()
    {       
        /*insert many objects to the ViewData dictionary*/     

        return View("Add");
    }

    [HttpPost]
    public ActionResult Add(Item myObj)
    {
        /*do some action*/

        ViewData["msg"] = "blabla";

        return View("Add");
    }
Community
  • 1
  • 1
Tony
  • 12,405
  • 36
  • 126
  • 226

2 Answers2

3

Try clearing the modelstate:

[HttpPost]
public ActionResult Add(Item myObj)
{
    ModelState.Clear();
    ViewData["msg"] = "blabla";
    return View("Add");
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
0

I would highly recommend implementing the PRG pattern in conjunction with TempData to display a message.

Charlino
  • 15,802
  • 3
  • 58
  • 74