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

前端 未结 4 1463
春和景丽
春和景丽 2020-12-30 02:41

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#, Enti

4条回答
  •  臣服心动
    2020-12-30 03:34

    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) { %>
      

    <%= Html.Encode(TempData["notice"]) %>

    <% } %>

    Or, in a Razor view:

    @if (TempData["notice"] != null) {
      

    @TempData["notice"]

    }

    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.

提交回复
热议问题