【问题标题】:Cant use TempData on Bootstrap无法在 Bootstrap 上使用 TempData
【发布时间】:2021-03-09 21:22:00
【问题描述】:

我试图在我的网络应用程序上显示 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

【问题讨论】:

    标签: c# model-view-controller bootstrap-4 controller


    【解决方案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 的答案。它只是另一种解决方案。

    【讨论】:

    • 但是...这不是同一个nuget吗?正确的??我在 BootstrapToastDataManager 上有错误。附言Bootstrap,作者:The Bootstrap Authors,Twitter Inc. 它说“不兼容,请改用 Bower(link)”
    • 在 BootstrapToastDataManager 中,我没有使用任何 nuget 包,你能分享显示错误的屏幕截图
    • AlertNotificationType 是一个 Enum,AlertViewModel 是一个简单的类。如果您想要该信息,请让我知道,我也可以分享
    • 我可以在哪里分享图片?是的,拜托,如果你可以上课
    • 代码更新缺少类声明
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-02
    • 1970-01-01
    • 1970-01-01
    • 2015-11-19
    • 2015-07-20
    • 1970-01-01
    相关资源
    最近更新 更多