I want to show success custom alert on my application. I got some answer from another thred. And I have applied it like this. Controller
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);
}
and the view
@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>
}
}
My concern is, it shows success message when directed to the index again. How can I show that within the create view and then directed to the index view?
CodePudding user response:
You can use the TempData[""] to check the status of your create/update method and if the TempData[""] is having some value then you can display what you wanted to display
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);
}
and now you can check the value of TempData["msg"] on your view
//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>
}
}
Or something what you have done is
@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>
}
}
}