In mu MVC application page I've a Product page (Controller: ProductController, Action: Index) which lists all the products from DB. Now I've a "Add Product" link which offers a form to fill Product details. On submitting the from AddProduct action is called which calls for a StoredProcedure (modelled in my model class). On succesful addition I use to RedirectAction("Index").Now my StoredProcedure returns me a message indicating the reslult of addition.I want this message to be stored in ViewData and show that on Index page. How can I do that? Any help would be much appreciated.
+5
A:
Use TempData
instead of ViewData
:
public ActionResult Index()
{
ViewData["message"] = TempData["message"];
return View();
}
public ActionResult AddProduct()
{
TempData["message"] = "product created";
return RedirectToAction("Index");
}
And in the Index view:
<% if (ViewData["message"] != null) { %>
<div><%= Html.Encode((string)ViewData["message"]) %></div>
<% } %>
Darin Dimitrov
2009-11-24 11:06:17