无法在 Bootstrap 上使用 TempData

Cant use TempData on Bootstrap

我试图在我的网络应用程序上显示 Bootstrap,我发现这个 bootstrap 做得很好的人...所以我决定使用它,但是在 NotificationExtensions.cs 我遇到了编译错误 CS1061

Success message from Controller to View

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

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

            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;//And here, same .TempData
        }
//...More code

我在 BaseController 的元数据中寻找它,但没有。 我正在使用 .NetCore 3.1

我有另一个解决方案来显示Bootstrap 4 Toast

public sealed class BootstrapToastDataManager
{
    public BootstrapToastDataManager AddMessage(AlertNotificationType alertNotificationType, string Message)
    {
        if (HttpContext.Current == null)
            throw new ApplicationException("SessionState is not active");

        List<AlertViewModel> messages = new List<AlertViewModel>();
        if(HttpContext.Current.Session["Alerts"] != null)
        {
            messages = HttpContext.Current.Session["Alerts"] as List<AlertViewModel>;
        }

        messages.Add(new AlertViewModel(alertNotificationType,Message ));

        HttpContext.Current.Session["Alerts"] = messages;
        return this;
    }

    public  List<AlertViewModel> GetMessages()
    {
        if (HttpContext.Current == null || HttpContext.Current.Session["Alerts"] == null)
            return null;

        var messages = HttpContext.Current.Session["Alerts"] as List<AlertViewModel>;

        /* Remove After Read */
        HttpContext.Current.Session["Alerts"] = null;

        return messages;
    }
}

Html 助手扩展

public static IHtmlString ShowToast(this HtmlHelper @this, AlertViewModel model)
    {
        if (model == null)
            return MvcHtmlString.Empty;

        var tosterStyle = "";

        switch (model.AlertNotificationType)
        {
            case AlertNotificationType.Success:
                tosterStyle = "toast-success";
                break;
            case AlertNotificationType.Info:
                tosterStyle = "toast-info";
                break;
            case AlertNotificationType.Warning:
                tosterStyle = "toast-warning";
                break;
            case AlertNotificationType.Error:
                tosterStyle = "toast-danger";
                break;
            default:
                break;
        }

        var toaster = new TagBuilder("div");
        toaster.AddCssClass($"toast {tosterStyle}"); //toast-position
        toaster.Attributes.Add("role", "alert");
        toaster.Attributes.Add("aria-live", "assertive");
        toaster.Attributes.Add("aria-atomic", "true");
        toaster.Attributes.Add("data-autohide", "true"); //delay
        toaster.Attributes.Add("data-delay", "5000"); //delay

        //var toasterHeader = new TagBuilder("div");
        //toasterHeader.AddCssClass($"toast-header");

        //var toasterHeaderImage = new TagBuilder("img");
        //toasterHeaderImage.AddCssClass("rounded mr-2");
        //toasterHeaderImage.Attributes.Add("width", "16");
        //toasterHeaderImage.Attributes.Add("height", "16");
        //toasterHeaderImage.Attributes.Add("src", VirtualPathUtility.ToAbsolute("~/favicon.ico"));

        var strong = new TagBuilder("strong");
        strong.AddCssClass("mr-auto");
        strong.InnerHtml = "Site Title <small>wants to inform you </small>";

        var small = new TagBuilder("small");
        small.AddCssClass("text-muted");
        small.InnerHtml = "";

        var toasterButton = new TagBuilder("button");
        toasterButton.AddCssClass("ml-2 mb-1 close");
        toasterButton.Attributes.Add("data-dismiss", "toast");
        toasterButton.Attributes.Add("aria-label", "Close");
        toasterButton.InnerHtml = @"<span aria-hidden='true'>&times;</span>";

        var toasterBody = new TagBuilder("div");
        toasterBody.AddCssClass("toast-body p-3");
        toasterBody.InnerHtml = model.Message;    

        StringBuilder toastHeaderBuilder = new StringBuilder();

        //toastHeaderBuilder.Append(toasterHeaderImage.ToString(TagRenderMode.SelfClosing));
        toastHeaderBuilder.Append(strong.ToString(TagRenderMode.Normal));
        toastHeaderBuilder.Append(small.ToString(TagRenderMode.Normal));
        toastHeaderBuilder.Append(toasterButton.ToString(TagRenderMode.Normal));

        //toasterHeader.InnerHtml = toastHeaderBuilder.ToString();


        StringBuilder toasterBodyBuilder = new StringBuilder();
        //toasterBodyBuilder.Append(toasterHeader.ToString(TagRenderMode.Normal));
        toasterBodyBuilder.Append(toasterBody.ToString(TagRenderMode.Normal));

        toaster.InnerHtml = toasterBodyBuilder.ToString();

        StringBuilder htmlBuilder = new StringBuilder();
        htmlBuilder.Append(toaster.ToString(TagRenderMode.Normal));
        var html = htmlBuilder.ToString();
        /*
        <div class="toast" role="alert" aria-live="assertive" aria-atomic="true" data-autohide="false" style="position: absolute; z-index:100; top: 60px; right: 10px;">
            <div class="toast-header">
                <img src="@Url.Content("~/favicon.ico")" class="rounded mr-2" alt="" width="16" height="16">
                <strong class="mr-auto">Just Thankz <small>wants to inform you </small> </strong>
                <small class="text-muted"> </small>
                <button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="toast-body">
                @TempData["Message"] @ViewData["Message"]
            </div>
        </div>
         */
        return MvcHtmlString.Create(html);
    }

电话来自 _Layout.cshtml

@{
        var alerts = new BootstrapToastDataManager().GetMessages();
        if (alerts != null)
        {
            <div aria-live="polite" aria-atomic="true" style="position: absolute; z-index:99; top:60px; right:10px; min-height: 200px;min-width:300px;">
                <div style="position: absolute; top: 0; right: 0;">
                    @foreach (var alert in alerts)
                    {
                        @Html.ShowToast(alert);
                    }
                </div>
            </div>
        }
    }

AlertNotificationType.cs

public enum AlertNotificationType : int
{
    Success,
    Info,
    Warning,
    Error
}

AlertViewModel.cs

public sealed class AlertViewModel
{
    public AlertNotificationType AlertNotificationType { get; private set; }
    public string Message { get; private set; }

    public AlertViewModel(AlertNotificationType alertNotificationType, string Message)
    {
        this.AlertNotificationType = alertNotificationType;
        this.Message = Message;
    }
}

并在某些事件发生时从控制器调用

_bootstrapDataManager
                .AddMessage(AlertNotificationType.Success, "This Is a Success Message!")
                .AddMessage(AlertNotificationType.Info, "An Information from Your Site!")
                .AddMessage(AlertNotificationType.Warning, "This Is a Warning Message!")
                .AddMessage(AlertNotificationType.Error, "This Is a Error Message!");

我在 .Net 中开发了它,但您可以在 .NetCore 中实现它。我知道这不是 OP 的答案。它只是另一种解决方案。