当我从另一个操作中单击后退箭头时,Tempdata 没有被清除?

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

我想在添加用户时显示警告消息。它发生得很顺利,但是当我从另一个操作中按浏览器的后退箭头时,它仍然显示警告消息。

//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>

在您的 Index 方法中,您可以使用这样的 OutputCacheAttribute 注释来禁用缓存

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

}

我认为原因是当您返回浏览器时,它会调用控制器并为 TempData["Success"]

存储值

在您的控制器中使用以下代码试试这个

public ActionResult Add(CreateViewModel objCreate)
{
   if (!IsPostBack){
      userRepo.AddUser( objCreate);
      TempData["Success"] = "User Added Successfully!";
   }
   return RedirectToAction("Index"); 
}