Search code examples
javascriptc#asp.netasp.net-mvctempdata

Tempdata not being cleared when i click back arrow from another action?


I want show the alert message when the user is added. It happens smoothly but when i press the back arrow of the browser from the another action it still shows the alert message.

//this is my partial view
<div class="row" id="alerts">
<div class="col-lg-12">
    @if (TempData["Success"] != null)
    {
        <div class="alert alert-success alert-dismissable">
            <button type="button" class="close" data-dismiss="alert" aria-
hidden="true">x</button>
            <h4><i class="icon fa fa-ban"></i> Alert!</h4>
            @TempData["Success"]
        </div>

    }
</div>
</div>

//this is my controller
 public ActionResult Add(CreateViewModel objCreate)
    {
        userRepo.AddUser( objCreate);

        TempData["Success"] = "User Added Successfully!";
        return RedirectToAction("Index");    
    }

//this is my view
<div class="col-md-10 col-md-offset-2">
            @Html.Partial("_Alerts")
            @RenderBody()
 </div>

Solution

  • In your Index method, you can disable caching by using OutputCacheAttribute annotation like this

    [OutputCacheAttribute(VaryByParam = "*", Duration = 0, NoStore = true)]
    public ActionResult Index()
    {
        // code of Index()
    
    }