本文介绍了如何在ASP MVC中重定向到另一个页面后保持Toastr Notification存活的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个ASP MVC 5应用程序,我正在向Toast的Notifaction显示一个问题。

I am developping an ASP MVC 5 application and I am heading up a problem with the showing of a Toast's Notifaction.

更新信息之后出现toast notifaction用户以确认操作成功。

The toast notifaction appears after updating the informations of a user in order to confirm the success of operation.

不幸的是,在加载下一页后,它会很快消失(2秒内)。我认为这是由于使用服务器端代码(在这种情况下为C#),整个页面重新加载,包括javascript文件。这就是为什么toastr也消失了。

Unfortunately it disappears quickly (in 2 seconds) after the loading of the next page. I think this is due to the using of server side code (C# in this case), where the entire page reloads, including the javascript files. This is why the toastr disappears too.

有没有办法让它保持更长的时间?

Is there any way to keep it a lil longer ?

我试图将必要的信息传递到下一页,以便第二页知道显示toastr而不是page1,但它不起作用。

I tried to o pass the necessary information to the next page, so that page two would know to display the toastr instead of page1 but it didn't work.

这是代码的片段:

查看:

 @using (Html.BeginForm("Edit", "Client", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
    {
        <input type="submit" value="Save" class="btn btn-default" onclick="Update()">
    }

        <script src="/template/web/js/jquery-2.2.3.min.js"></script>
        <script src="~/Scripts/toastr.js"></script>
        <script>

            function Update() {
                toastr.success("Your infos are updated with succes !");
            }
        </script>

控制器:

[HttpPost]
    public ActionResult Edit(int id, Client cmodel)
    {
        try
        {

                ClientManagement cdb = new ClientManagement();
            if (cdb.UpdateDetails(cmodel))
            {
                ViewBag.Message = "Client Details Edited Successfully";
            }

            return RedirectToAction("Profil");
        }
        catch
        {
           return View();
        }
    }

Profil:

 public ActionResult Profil()
    {
        ClientManagement dbhandle = new ClientManagement();
        ViewBag.Message = TempData["Message"];
        return View(dbhandle.GetClientsInfo());
    }

在查看Profil(页面的底部):

In View Profil (buttom of page) :

 <link href="~/Content/toastr.min.css" rel="stylesheet" />
script src="~/scripts/toastr.js"></script>
        <script src="~/scripts/toastr.min.js"></script>

     @if (ViewBag.Message != null)
    {
        <script type="text/javascript">toastr.success("@ViewBag.Message");</script>
    }


推荐答案

如果你想传递 一次性消息 ,您需要使用 TempData

If you want to pass one-time message from an action method to another action method, you will need to use TempData.

[HttpPost]
public ActionResult Edit(int id, Client cmodel)
{
   try
   {
      ClientManagement cdb = new ClientManagement();
      if (cdb.UpdateDetails(cmodel))
      {
         TempData["Message"] = "Client Details Edited Successfully";
      }
      return RedirectToAction("Profil");
   }
   catch
   {
      return View(cmodel);
   }
}

然后您可以在下一页内部检索它方法或观点。请注意,在您阅读后,它会在请求结束时清除。

You then retrieve it at the next page either inside action method or view. Please note that it would be cleared out at the end of the request after you read it.

Profil.cshtml

@if (TempData["Message"] != null)
{
   <script type="text/javascript">toastr.success("@TempData["Message"]");</script>
}



更新



Update

以上代码有效。如果您使用默认的ASP.NET MVC布局,请确保将脚本放在 Scripts 部分中,以便在jQuery之后呈现。

Above code works. If you use default ASP.NET MVC Layout, please make sure you place the script inside Scripts section, so that it renders after jQuery.

@section Scripts {
    <script src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/js/toastr.min.js"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/css/toastr.min.css" rel="stylesheet" />
    @if (TempData["Message"] != null)
    {
        <script type="text/javascript">toastr.success("@TempData["Message"]");</script>
    }
}

FYI:如果你在其他地方使用toastr在某些地方,您可能需要考虑使用其他脚本和样式捆绑和缩小它们。

这篇关于如何在ASP MVC中重定向到另一个页面后保持Toastr Notification存活的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 20:37