【问题标题】:Custom alert message in asp.MVCasp.MVC 中的自定义警报消息
【发布时间】:2021-11-05 05:16:52
【问题描述】:

我想在我的应用程序上显示成功自定义警报。 我从另一个线程中得到了一些答案。我已经这样应用了。控制器

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

    }

我担心的是,当再次定向到索引时,它会显示成功消息。如何在创建视图中显示它然后定向到索引视图?

【问题讨论】:

    标签: javascript asp.net asp.net-mvc asp.net-mvc-4


    【解决方案1】:

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

    【讨论】:

    • 如果您认为这个问题是重复的,请标记或投票关闭它。不要发布仅包含指向其他答案的链接的答案。
    • 好的,我会在回答问题时格外小心。谢谢
    猜你喜欢
    • 2014-02-27
    • 1970-01-01
    • 1970-01-01
    • 2020-02-18
    • 1970-01-01
    • 2016-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多