c# - 从 Controller 到 View 的成功消息

标签 c# asp.net asp.net-mvc-4 notifications

目标

我想在添加某些用户时在我的 View 中显示一些消息。

问题

当我们的模型出现问题时,有一个方法 (ModelState.AddModelError) 来处理不成功的消息。但是,当一切顺利时,我们如何处理向用户发送的消息,表明他的操作已成功?

我找到了 this thread提供了一个解决方案,但大约三年过去了,我需要知道:没有另一种方法,也许更成熟?并不是说这不是,但我们仍然以同样的方式处理成功消息?

最佳答案

Brad Christie's 展开answer ,我创建了一个 NuGet 包,BootstrapNotifications ,它将通过内置的 Bootstrap3 支持为您完成此操作。此包还支持多种通知类型(错误、警告、成功和信息),带有预先设计的警报,并且易于扩展。

该扩展支持同一类型和不同类型的每个请求的多个通知。

代码

NotificationExtensions.cs:

public static class NotificationExtensions
{
    private static IDictionary<String, String> NotificationKey = new Dictionary<String, String>
    {
        { "Error",      "App.Notifications.Error" }, 
        { "Warning",    "App.Notifications.Warning" },
        { "Success",    "App.Notifications.Success" },
        { "Info",       "App.Notifications.Info" }
    };


    public static void AddNotification(this ControllerBase controller, String message, String notificationType)
    {
        string NotificationKey = getNotificationKeyByType(notificationType);
        ICollection<String> messages = controller.TempData[NotificationKey] as ICollection<String>;

        if (messages == null)
        {
            controller.TempData[NotificationKey] = (messages = new HashSet<String>());
        }

        messages.Add(message);
    }

    public static IEnumerable<String> GetNotifications(this HtmlHelper htmlHelper, String notificationType)
    {
        string NotificationKey = getNotificationKeyByType(notificationType);
        return htmlHelper.ViewContext.Controller.TempData[NotificationKey] as ICollection<String> ?? null;
    }

    private static string getNotificationKeyByType(string notificationType)
    {
        try
        {
            return NotificationKey[notificationType];
        }
        catch (IndexOutOfRangeException e)
        {
            ArgumentException exception = new ArgumentException("Key is invalid", "notificationType", e);
            throw exception;
        }
    }
}

public static class NotificationType
{
    public const string ERROR = "Error";
    public const string WARNING = "Warning";
    public const string SUCCESS = "Success";
    public const string INFO = "Info";

}

_Notifications.cshtml:

@using YourApp.Extensions
@{
    var errorList = Html.GetNotifications(NotificationType.ERROR);
    var warningList = Html.GetNotifications(NotificationType.WARNING);
    var successList = Html.GetNotifications(NotificationType.SUCCESS);
    var infoList = Html.GetNotifications(NotificationType.INFO);
}
<!-- display errors -->
@if (errorList != null)
{
    <div class="alert alert-danger alert-dismissable">
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
        @if(errorList.Count() > 1){
            <strong><span class="glyphicon glyphicon-remove"></span> There are @errorList.Count() errors: </strong>
            <ul>
                @foreach (String message in errorList)
                {
                    <li>@Html.Raw(message)</li>
                }
            </ul>
        }
        else{
            <strong><span class="glyphicon glyphicon-remove"></span> Error: </strong>
            @Html.Raw(errorList.First())
        }
    </div>
}

<!-- display warnings -->
@if (warningList != null)
{
    <div class="alert alert-warning alert-dismissable">
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
        @if(warningList.Count() > 1){
            <strong><span class="glyphicon glyphicon-warning-sign"></span> There are @warningList.Count() warnings: </strong>
            <ul>
                @foreach (String message in warningList)
                {
                    <li>@Html.Raw(message)</li>
                }
            </ul>
        }
        else{
            <strong><span class="glyphicon glyphicon-warning-sign"></span> Warning: </strong>
            @Html.Raw(warningList.First())
        }
    </div>
}

<!-- display success -->
@if (successList != null)
{
    <div class="alert alert-success alert-dismissable">
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
        @if(successList.Count() > 1){
            <strong><span class="glyphicon glyphicon-ok"></span> There are @successList.Count() successful notifications: </strong>
            <ul>
                @foreach (String message in successList)
                {
                    <li>@Html.Raw(message)</li>
                }
            </ul>
        }
        else{
            <strong><span class="glyphicon glyphicon-ok"></span> Success! </strong>
            @Html.Raw(successList.First())
        }
    </div>
}

<!-- display success -->
@if (infoList != null)
{
    <div class="alert alert-info alert-dismissable">
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
        @if(infoList.Count() > 1){
            <strong><span class="glyphicon glyphicon-info-sign"></span> There are @infoList.Count() notifications: </strong>
            <ul>
                @foreach (String message in infoList)
                {
                    <li>@Html.Raw(message)</li>
                }
            </ul>
        }
        else{
            <strong><span class="glyphicon glyphicon-info-sign"></span> </strong>
            @Html.Raw(infoList.First())
        }
    </div>
}

要查看所有这些代码及其使用方法,您可以从 github 下载完整的工作演示。 .

关于c# - 从 Controller 到 View 的成功消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18337914/

相关文章:

c# - MVC 4 Ajax.Action 链接不起作用

c# - IEnumerable<T>.Intersect<T> 中的 Type 参数有什么作用?

c# - 构建成功但发布失败——此版本的Microsoft.AspNetCore.All只兼容netcoreapp2.2目标框架

c# - 如何从数组中删除元素

c# - Asp.Net MVC - 参数与模型值的绑定(bind)!

javascript - 如果 ActionResult 已完成,如何打印结果

c# - C++/CLI 中 C# 的静态类/方法的等价物

c# - Entity Framework 4 升级问题,不允许同时按名称和属性加载

c# - 发送带重音的参数c#

asp.net-mvc - 值不能为 null 或为空。参数名称: contentPath