【问题标题】:Notification message Asp.net Core通知消息 Asp.net Core
【发布时间】:2020-02-29 01:14:45
【问题描述】:

我试图在剃刀页面中的 OnPost 之后显示通知。因为要刷新页面来更新页面数据,所以不能用return Page(),必须用return RedirectToPage()

因此,[Tempdata] 概念不起作用并且消息为空,因为它再次通过 OnGet。

有什么想法可以解决这个问题吗?这是我的代码:

     public async Task<IActionResult> OnPostAddPerson()
    {
        try
        {
            if (ModelState.IsValid)
            {
                await _repo.AddPerson(person);
                Message = "Success";

            }
            else
            {
                Message = "An Error occured";
            }

        }
        catch(Exception ex)
        {
            throw ex;
        }

        return RedirectToPage("/Index");
    }

【问题讨论】:

  • 只用你的模型返回 View("Index")
  • 顺便说一句...throw ex; 不会为您做任何事情,实际上会从异常中删除有用的信息。这是一个值得打破的习惯。如果你的 catch 块没有做任何事情,那么它应该被完全删除。如果您保留它只是为了暂停调试器(尽管还有其他方法可以针对异常执行此操作),那么至少养成使用 throw; 重新抛出原始异常而不是生成的习惯一个新的堆栈跟踪。
  • @David 感谢您的回答,它只是一个占位符!我通常在文本文件中记录我的异常
  • @godot 猜测这是可能的,但只需要再次调用数据库中的所有获取

标签: c# .net-core


【解决方案1】:

为此,您需要安装名为

的第三方包
Install-Package NToastNotify

在这个包中,您将获得服务器端 toast 通知渲染。包括Ajax Call、XMLHTTPRequests。

using NToastNotify.Libraries;


services.AddMvc().AddNToastNotifyToastr(new ToastrOptions()
{
            ProgressBar = false,
            PositionClass = ToastPositions.BottomCenter
});

//Or simply go 
services.AddMvc().AddNToastNotifyToastr();

添加中间件

 public void Configure(IApplicationBuilder app, IHostingEnvironment env)
 {
        //NOTE this line must be above .UseMvc() line.
        app.UseNToastNotify();

        app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
 }

在您的 html 文件中添加以下行。最好在您的布局页面中。

@await Component.InvokeAsync("NToastNotify")

添加您的 toast 消息。

public class HomeController : Controller
    {
        private readonly IToastNotification _toastNotification;

        public HomeController(IToastNotification toastNotification)
        {
            _toastNotification = toastNotification;
        }
        public IActionResult Index()
        {
            //Testing Default Methods

            //Success
            _toastNotification.AddSuccessToastMessage("Same for success message");
            // Success with default options (taking into account the overwritten defaults when initializing in Startup.cs)
            _toastNotification.AddSuccessToastMessage();

            //Info
            _toastNotification.AddInfoToastMessage();

            //Warning
            _toastNotification.AddWarningToastMessage();

            //Error
            _toastNotification.AddErrorToastMessage();

            return View();
        }

        public IActionResult About()
        {
            _toastNotification.AddInfoToastMessage("You got redirected");
            return View();
        }

        public IActionResult Contact()
        {
            _toastNotification.AddAlertToastMessage("You will be redirected");
            return RedirectToAction("About");
        }

        public IActionResult Error()
        {
            _toastNotification.AddErrorToastMessage("There was something wrong with this request.");
            return View();
        }

        public IActionResult Empty()
        {

            return View();
        }

        public IActionResult Ajax()
        {
            _toastNotification.AddInfoToastMessage("This page will make ajax requests and show notifications.");
            return View();
        }

        public IActionResult AjaxCall()
        {
            System.Threading.Thread.Sleep(2000);
            _toastNotification.AddSuccessToastMessage("This toast is shown on Ajax request. AJAX CALL " + DateTime.Now.ToLongTimeString());
            return PartialView("_PartialView", "Ajax Call");
        }

        public IActionResult NormalAjaxCall()
        {
            return PartialView("_PartialView", "Normal Ajax Call");
        }

        public IActionResult ErrorAjaxCall()
        {
            throw new Exception("Error occurred");
        }
    }

【讨论】:

  • 无法让它在剃刀页面上工作。猜测它仅适用于 mvc
  • 这是该库作者的声明。 javascript toastr 的 Dot Net 抽象在 ASP.NET Core MVC 项目上创建 toast 通知
  • 作者在这里,适用于剃须刀页面和 MVC 项目。有关 github 存储库中的示例,请参阅示例文件夹。
【解决方案2】:

您需要返回 JSON 对象。因为它最广泛用于返回对象为

【讨论】:

    猜你喜欢
    • 2020-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-23
    • 2021-01-05
    • 1970-01-01
    • 2017-03-15
    • 2017-04-02
    相关资源
    最近更新 更多