Tony
Tony

Reputation: 12695

How to re-render a View without inserting data into fields?

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

Upvotes: 0

Views: 526

Answers (2)

ten5peed
ten5peed

Reputation: 15890

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

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038940

Try clearing the modelstate:

[HttpPost]
public ActionResult Add(Item myObj)
{
    ModelState.Clear();
    ViewData["msg"] = "blabla";
    return View("Add");
}

Upvotes: 3

Related Questions