我想在添加用戶時顯示警報消息。它會順利發生,但是當我從另一個動作中按下瀏覽器的後退箭頭時,它仍然顯示警報消息。當我點擊另一個動作的箭頭時Tempdata沒有被清除?
//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>
謝謝!它工作正常,但它仍然是一個好方法嗎? –
@AnandShrestha將取決於您的需求,但上面的示例將禁用該特定操作的緩存,這是您在重定向到索引後單擊某處並在瀏覽器上單擊「返回」按鈕時發生的情況。 [Here](https://msdn.microsoft.com/en-us/library/system.web.mvc.outputcacheattribute(v = vs.118).aspx)是相關的文檔和[另一個相關的SO問題](http ://stackoverflow.com/questions/20895489/outputcache-setting-inside-my-asp-net-mvc-web-application-multiple-syntax-to-pr)。希望能幫助到你。 – granit