我想在我的應用程式上顯示成功自定義警報。我從另一個執行緒中得到了一些答案。我已經這樣應用了。控制器
public ActionResult Create([Bind(Include = "Id,SuppName,Pay_Method,Status,Create_By,Create_Date")] M_Supplier m_Supplier)
{
if (ModelState.IsValid)
{
m_Supplier.Create_By= int.Parse(((System.Security.Claims.ClaimsIdentity)User.Identity).FindFirst("UserId").Value);
m_Supplier.Status = true;
m_Supplier.Create_Date = DateTime.Now;
db.M_Supplier.Add(m_Supplier);
db.SaveChanges();
return RedirectToAction("Index", new { ac = "success" });
}
return View(m_Supplier);
}
和視圖
@Html.ActionLink("Back to List", "Index")
@{
var parameter = Request.QueryString["ac"];
//Check parameter here and display Message
if (parameter == "success")
{
<div class="alert alert-success alert-dismissible">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
<strong><i class="fa fa-exclamation-triangle" aria-hidden="true"></i> Record Added Successfully.</strong>
</div>
}
}
我擔心的是,當再次定向到索引時,它會顯示成功訊息。如何在創建視圖中顯示它然后定向到索引視圖?
uj5u.com熱心網友回復:
您可以使用 TempData[""] 檢查創建/更新方法的狀態,如果 TempData[""] 有一些值,那么您可以顯示您想要顯示的內容
public ActionResult Create([Bind(Include = "Id,SuppName,Pay_Method,Status,Create_By,Create_Date")] M_Supplier m_Supplier)
{
if (ModelState.IsValid)
{
m_Supplier.Create_By= int.Parse(((System.Security.Claims.ClaimsIdentity)User.Identity).FindFirst("UserId").Value);
m_Supplier.Status = true;
m_Supplier.Create_Date = DateTime.Now;
db.M_Supplier.Add(m_Supplier);
db.SaveChanges();
TempData["msg"]="success";
return RedirectToAction("Index");
}
TempData["msg"]="error";
return View(m_Supplier);
}
現在您可以在視圖中檢查 TempData["msg"] 的值
//Check parameter here and display Message
@if (TempData["msg"] !=null)
{
if(TempData["msg"].ToString()=="success"){
<div class="alert alert-success alert-dismissible">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
<strong><i class="fa fa-exclamation-triangle" aria-hidden="true"></i> Record Added Successfully.</strong>
</div>
}
}
或者你所做的事情是
@Html.ActionLink("Back to List", "Index")
@{
if (TempData["msg"] !=null)
{
if(TempData["msg"].ToString()=="success"){
<div class="alert alert-success alert-dismissible">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
<strong><i class="fa fa-exclamation-triangle" aria-hidden="true"></i> Record Added Successfully.</strong>
</div>
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/420654.html
標籤:
