2017-04-06 4 views
0

ユーザーが追加されたときに警告メッセージを表示します。それはスムーズに起こりますが、別のアクションからブラウザの戻る矢印を押すと、警告メッセージが表示されます。あなたのIndexメソッドで他のアクションから戻る矢印をクリックすると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(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 = 118).aspx)は、関連するドキュメントであり、[関連するその他の質問](http ://stackoverflow.com/questions/20895489/outputcache-setting-inside-my-asp-net-mvc-web-application-multiple-syntax-to-pr)。それが役に立てば幸い。 – granit

0

ようOutputCacheAttribute注釈を使用してキャッシュを無効にすることができ、私はあなたがブラウザ上でバックに行くときの理由は、それが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 = 1.10).aspx – Thili77

関連する問題