Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show Alert Message like "successfully Inserted" after inserting to DB using ASp.net MVC3

How to write a code for displaying the alert message: "Successfully registered", after user data is stored in database, using MVC

I am using Asp.Net MVC3, C#, Entity Model.

like image 719
user581007 Avatar asked Nov 09 '11 10:11

user581007


1 Answers

Try using TempData:

public ActionResult Create(FormCollection collection) {
  ...
  TempData["notice"] = "Successfully registered";
  return RedirectToAction("Index");
  ...
}

Then, in your Index view, or master page, etc., you can do this:

<% if (TempData["notice"] != null) { %>
  <p><%= Html.Encode(TempData["notice"]) %></p>
<% } %>

Or, in a Razor view:

@if (TempData["notice"] != null) {
  <p>@TempData["notice"]</p>
}

Quote from MSDN (page no longer exists as of 2014, archived copy here):

An action method can store data in the controller's TempDataDictionary object before it calls the controller's RedirectToAction method to invoke the next action. The TempData property value is stored in session state. Any action method that is called after the TempDataDictionary value is set can get values from the object and then process or display them. The value of TempData persists until it is read or until the session times out. Persisting TempData in this way enables scenarios such as redirection, because the values in TempData are available beyond a single request.

like image 115
Mike Mertsock Avatar answered Oct 13 '22 19:10

Mike Mertsock