Exemplo n.º 1
0
        /// <summary>
        /// Render all messages that have been set during execution of the controller action.
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <returns></returns>
        public static HtmlString RenderMessages(this HtmlHelper htmlHelper, MessageDialogType type = MessageDialogType.AdminLTE)
        {
            var messages = String.Empty;

            foreach (var messageType in Enum.GetNames(typeof(MessageType)))
            {
                var message = htmlHelper.ViewContext.ViewData.ContainsKey(messageType)
                                ? htmlHelper.ViewContext.ViewData[messageType]
                                : htmlHelper.ViewContext.TempData.ContainsKey(messageType)
                                    ? htmlHelper.ViewContext.TempData[messageType]
                                    : null;
                if (message != null)
                {
                    var lowermessage = messageType.ToLowerInvariant();

                    MessageType msgType = (MessageType)Enum.Parse(typeof(MessageType), messageType, true);

                    var messageTitle = msgType.GetDisplayValue();

                    var messageBoxBuilder = new TagBuilder("div");

                    if (type == MessageDialogType.BootStrap)
                    {
                        messageBoxBuilder.AddCssClass(string.Format("alert alert-{0} fade in", lowermessage));
                        messageBoxBuilder.InnerHtml = string.Format("<button class='close' data-dismiss='alert' aria-label='close'>&times;</button><strong>{0}</strong> {1}", messageTitle, message.ToString());
                    }
                    else
                    {
                        messageBoxBuilder.AddCssClass(string.Format("alert alert-{0} fade in", lowermessage));
                        string msgicon = "";
                        switch (lowermessage)
                        {
                        case "danger":
                            msgicon = "fa-ban";
                            break;

                        case "info":
                            msgicon = "fa-info";
                            break;

                        case "warning":
                            msgicon = "fa-warning";
                            break;

                        case "success":
                            msgicon = "fa-check";
                            break;

                        default:
                            break;
                        }
                        messageBoxBuilder.InnerHtml =
                            string.Format("<button type = 'button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>" +
                                          "<h4><i class='icon fa {0}'></i> {1}</h4>{2}", msgicon, messageTitle, message.ToString());
                    }
                    messages += messageBoxBuilder.ToString();
                }
            }
            return(MvcHtmlString.Create(messages));
        }