简体   繁体   中英

Custom alert message in ASP.NET MVC

I want to show success custom alert on my application.

I got some answer from another thread. 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 this is 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">&times;</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?

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">&times;</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">&times;</a>
       <strong><i class="fa fa-exclamation-triangle" aria-hidden="true"></i> Record Added Successfully.</strong>
        </div>
       }

    }        

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM