2017-08-24 70 views
0

我試圖在存儲過程後顯示成功/未成功的消息。我保存ActionResult調用保存按鈕單擊 - 然而,在我重定向回到索引我想顯示一個消息框。有沒有辦法首先返回視圖,然後RedirectToAction?顯示警告消息:在確定按鈕上單擊RedirectToAction

保存按鈕點:

[HttpPost] 
    public ActionResult Save(string submit, Models.KnownIssues knownIssue) 
    {   

     UpdateKnownIssue(knownIssue); 
     InsertKnownIssue(knownIssue); 


     return RedirectToAction("Index"); 

    } 

的viewbag警報:

public ActionResult Edit(KnownIssues knownIssue, string submit) 
    { 
     if (UpdateKnownIssue(knownIssue)) 
     { 
      ViewBag.ShowAlert = "<script>alert('Successfully Edited'); window.location.href = '/KnownIssues';</script>"; 
     } else 
     { 
      ViewBag.ShowAlert = "<script>alert('Unseccessful. Try again.');</script>"; 
     } 
     return View(knownIssue); 
    } 

回答

1

您可以將您的表單提交是一個Ajax表單提交,並返回一個JSON響應從你的操作方法了。在您的ajax電話的成功事件中,您可以將消息顯示給用戶,然後使用javascript進行重定向。

你可以使用下面的jQuery代碼來填表你的表單提交。

$(function() { 

    $("form").submit(function(e) { 
     e.preventDefault(); 
     var f = $(this); 
     $.post(f.attr("action"), f.serialize(),function(res) { 
      if(res.Status==="success") 
      { 
       alert(res.Message); 
       window.location.href="your new url here"; 
      } 
     }); 
    }); 

}); 

,並已動作方法更新,以返回JSON響應

[HttpPost] 
public ActionResult Save(string submit, Models.KnownIssues knownIssue) 
{ 
    // your code 
    if (Request.IsAjaxRequest()) 
    { 
     return Json(new {Status = "success", Message="Succesfully updated"}); 
    } 
    return RedirectToAction("Index"); 
} 

另一種選擇是顯示下一頁的消息。爲此,您可以使用當前的表單提交方法(不需要ajax)並使用TempData存儲郵件。在下一個操作方法中,讀取視圖中的TempData並將消息顯示給用戶。看看下面的職位代碼示例

Display message in a toastr after controller method finish

0

試試這個:

return RedirectToAction("Index", "Home", new { ac = "success" }); 

在索引視圖做(注:我使用引導警報):

@{ 
    var parameter = Request.QueryString["ac"]; 
    //Check parameter here and display Message 
    if (parameter == "success") 
    { 
     <div class="alert alert-success alert-dismissable"> 
      <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a> 
      <strong><i class="fa fa-exclamation-triangle" aria-hidden="true"></i> Record Added Successfully.</strong> 
     </div> 
    } 
} 
相關問題