简体   繁体   English

ASP.NET MVC 中的自定义警报消息

[英]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 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您可以使用 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);
    }

and now you can check the value of TempData["msg"] on your view现在您可以在视图中检查 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">&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>
       }

    }        

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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