2017-04-06 71 views
0

我想在添加用户时显示警报消息。它会顺利发生,但是当我从另一个动作中按下浏览器的后退箭头时,它仍然显示警报消息。当我点击另一个动作的箭头时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> 

回答

0

索引中的方法,你可以通过使用OutputCacheAttribute注释这样

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

} 
+0

谢谢!它工作正常,但它仍然是一个好方法吗? –

+0

@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

0

禁用缓存我想原因是当你去回浏览器,它呼叫控制器和存储值TempData["Success"]

在你的控制器试试这个使用下面的代码

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

它给出错误“名称'IsPostBack'在当前上下文中不存在” –

+0

添加引用检查此 - https://msdn.microsoft.com/en-us/library/system.web.ui.page.ispostback (v = vs.110)的.aspx – Thili77

相关问题