简体   繁体   English

来自控制器的警报消息:在局部视图中添加警报行为?

[英]Alert messages from controller: Add alert behavior in partial view?

In MVC 4, you could install this package Twitter.bootstrap.mvc and this would add lots of HTML helpers. 在MVC 4中,您可以安装此程序包Twitter.bootstrap.mvc ,这将添加许多HTML帮助器。

Once istalled you could send alert to view right from controller. 一旦安装完毕,您可以发送警报以从控制器直接查看。

For example: 例如:

public class AccountController : BaseController 
{
    public ActionResult AlertExample()
    {
       Success("This is a success alert");
       Error("This is error alert");
       Information("This is information alert");
       ...
       etc.
    }
}

This would send the success alert right from controller to the view. 这会将成功警报从控制器发送到视图。

Objective: Sending Growl Messages from controller 目标:从控制器发送低吼消息

I've tried to implement same thing from the project I mentioned. 我试图从我提到的项目中实现同样的事情。

So, this is what I've added to my project. 因此,这就是我添加到项目中的内容。

Base controller that other controller derives from 其他控制器派生的基本控制器

   public class BaseController : Controller
   {
        //
        // GET: /Base/
        public void Warning(string message)
        {
            TempData.Add(Alerts.WARNING, message);
        }
        public void Success(string message)
        {
            TempData.Add(Alerts.SUCCESS, message);
        }

        public void Information(string message)
        {
            TempData.Add(Alerts.INFORMATION, message);
        }

        public void Error(string message)
        {
            TempData.Add(Alerts.ERROR, message);
        }
    }

ControlGroupExtensionClass ControlGroupExtensionClass

    namespace BootstrapSupport
    {
        public class ControlGroup : IDisposable
        {
            private readonly HtmlHelper _html;

            public ControlGroup(HtmlHelper html)
            {
                _html = html;
            }

            public void Dispose()
            {
                _html.ViewContext.Writer.Write(_html.EndControlGroup());
            }
        }

    public static class ControlGroupExtensions
    {
        public static IHtmlString BeginControlGroupFor<T>(this HtmlHelper<T> html, Expression<Func<T, object>> modelProperty)
        {
            return BeginControlGroupFor(html, modelProperty, null);
        }

        public static IHtmlString BeginControlGroupFor<T>(this HtmlHelper<T> html, Expression<Func<T, object>> modelProperty, object htmlAttributes)
        {
            return BeginControlGroupFor(html, modelProperty,
                                        HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
        }

        public static IHtmlString BeginControlGroupFor<T>(this HtmlHelper<T> html, Expression<Func<T, object>> modelProperty, IDictionary<string, object> htmlAttributes)
        {
            string propertyName = ExpressionHelper.GetExpressionText(modelProperty);
            return BeginControlGroupFor(html, propertyName, null);
        }

        public static IHtmlString BeginControlGroupFor<T>(this HtmlHelper<T> html, string propertyName)
        {
            return BeginControlGroupFor(html, propertyName, null);
        }

        public static IHtmlString BeginControlGroupFor<T>(this HtmlHelper<T> html, string propertyName, object htmlAttributes)
        {
            return BeginControlGroupFor(html, propertyName, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
        }

        public static IHtmlString BeginControlGroupFor<T>(this HtmlHelper<T> html, string propertyName, IDictionary<string, object> htmlAttributes)
        {
            var controlGroupWrapper = new TagBuilder("div");
            controlGroupWrapper.MergeAttributes(htmlAttributes);
            controlGroupWrapper.AddCssClass("control-group");
            string partialFieldName = propertyName;
            string fullHtmlFieldName =
                html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(partialFieldName);
            if (!html.ViewData.ModelState.IsValidField(fullHtmlFieldName))
            {
                controlGroupWrapper.AddCssClass("error");
            }
            string openingTag = controlGroupWrapper.ToString(TagRenderMode.StartTag);
            return MvcHtmlString.Create(openingTag);
        }

        public static IHtmlString EndControlGroup(this HtmlHelper html)
        {
            return MvcHtmlString.Create("</div>");
        }

        public static ControlGroup ControlGroupFor<T>(this HtmlHelper<T> html, Expression<Func<T, object>> modelProperty)
        {
            return ControlGroupFor(html, modelProperty, null);
        }

        public static ControlGroup ControlGroupFor<T>(this HtmlHelper<T> html, Expression<Func<T, object>> modelProperty, object htmlAttributes)
        {
            string propertyName = ExpressionHelper.GetExpressionText(modelProperty);
            return ControlGroupFor(html, propertyName, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
        }

        public static ControlGroup ControlGroupFor<T>(this HtmlHelper<T> html, string propertyName)
        {
            return ControlGroupFor(html, propertyName, null);
        }

        public static ControlGroup ControlGroupFor<T>(this HtmlHelper<T> html, string propertyName, object htmlAttributes)
        {
            return ControlGroupFor(html, propertyName, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
        }

        public static ControlGroup ControlGroupFor<T>(this HtmlHelper<T> html, string propertyName, IDictionary<string, object> htmlAttributes)
        {
            html.ViewContext.Writer.Write(BeginControlGroupFor(html, propertyName, htmlAttributes));
            return new ControlGroup(html);
        }
    }

    public static class Alerts
    {
        public const string SUCCESS = "success";
        public const string WARNING = "warning";
        public const string ERROR = "error";
        public const string INFORMATION = "info";

        public static string[] ALL
        {
            get { return new[] { SUCCESS, WARNING, INFORMATION, ERROR }; }
        }
    }
}

ViewHelperExtensionClass ViewHelperExtensionClass

namespace BootstrapSupport
{
    public static class DefaultScaffoldingExtensions
    {
        public static string GetControllerName(this Type controllerType)
        {
            return controllerType.Name.Replace("Controller", String.Empty);
        }

        public static string GetActionName(this LambdaExpression actionExpression)
        {
            return ((MethodCallExpression)actionExpression.Body).Method.Name;
        }

        public static PropertyInfo[] VisibleProperties(this IEnumerable Model)
        {
            var elementType = Model.GetType().GetElementType();
            if (elementType == null)
            {
                elementType = Model.GetType().GetGenericArguments()[0];
            }
            return elementType.GetProperties().Where(info => info.Name != elementType.IdentifierPropertyName()).ToArray();
        }

        public static PropertyInfo[] VisibleProperties(this Object model)
        {
            return model.GetType().GetProperties().Where(info => info.Name != model.IdentifierPropertyName()).ToArray();
        }

        public static RouteValueDictionary GetIdValue(this object model)
        {
            var v = new RouteValueDictionary();
            v.Add(model.IdentifierPropertyName(), model.GetId());
            return v;
        }

        public static object GetId(this object model)
        {
            return model.GetType().GetProperty(model.IdentifierPropertyName()).GetValue(model, new object[0]);
        }


        public static string IdentifierPropertyName(this Object model)
        {
            return IdentifierPropertyName(model.GetType());
        }

        public static string IdentifierPropertyName(this Type type)
        {
            if (type.GetProperties().Any(info => info.PropertyType.AttributeExists<System.ComponentModel.DataAnnotations.KeyAttribute>()))
            {
                return
                    type.GetProperties().First(
                        info => info.PropertyType.AttributeExists<System.ComponentModel.DataAnnotations.KeyAttribute>())
                        .Name;
            }
            else if (type.GetProperties().Any(p => p.Name.Equals("id", StringComparison.CurrentCultureIgnoreCase)))
            {
                return
                    type.GetProperties().First(
                        p => p.Name.Equals("id", StringComparison.CurrentCultureIgnoreCase)).Name;
            }
            return "";
        }

        public static string GetLabel(this PropertyInfo propertyInfo)
        {
            var meta = ModelMetadataProviders.Current.GetMetadataForProperty(null, propertyInfo.DeclaringType, propertyInfo.Name);
            return meta.GetDisplayName();
        }

        public static string ToSeparatedWords(this string value)
        {
            return Regex.Replace(value, "([A-Z][a-z])", " $1").Trim();
        }

    }

    public static class PropertyInfoExtensions
    {
        public static bool AttributeExists<T>(this PropertyInfo propertyInfo) where T : class
        {
            var attribute = propertyInfo.GetCustomAttributes(typeof(T), false)
                                .FirstOrDefault() as T;
            if (attribute == null)
            {
                return false;
            }
            return true;
        }

        public static bool AttributeExists<T>(this Type type) where T : class
        {
            var attribute = type.GetCustomAttributes(typeof(T), false).FirstOrDefault() as T;
            if (attribute == null)
            {
                return false;
            }
            return true;
        }

        public static T GetAttribute<T>(this Type type) where T : class
        {
            return type.GetCustomAttributes(typeof(T), false).FirstOrDefault() as T;
        }

        public static T GetAttribute<T>(this PropertyInfo propertyInfo) where T : class
        {
            return propertyInfo.GetCustomAttributes(typeof(T), false).FirstOrDefault() as T;
        }

        public static string LabelFromType(Type @type)
        {
            var att = GetAttribute<DisplayNameAttribute>(@type);
            return att != null ? att.DisplayName
                : @type.Name.ToSeparatedWords();
        }

        public static string GetLabel(this Object Model)
        {
            return LabelFromType(Model.GetType());
        }

        public static string GetLabel(this IEnumerable Model)
        {
            var elementType = Model.GetType().GetElementType();
            if (elementType == null)
            {
                elementType = Model.GetType().GetGenericArguments()[0];
            }
            return LabelFromType(elementType);
        }
    }

    //public static class HtmlHelperExtensions
    //{
    //    public static MvcHtmlString TryPartial(this HtmlHelper helper, string viewName, object model)
    //    {
    //        try
    //        {
    //            return helper.Partial(viewName, model);
    //        }
    //        catch (Exception)
    //        {
    //        }
    //        return MvcHtmlString.Empty;
    //    }
    //}
}

and the _alert partial View _alert部分视图

@using BootstrapSupport
@if (TempData.ContainsKey(Alerts.WARNING))
{
    <div class="alert alert-block">
        <a class="close" data-dismiss="alert" href="#">x</a>
        <h4 class="toast-title">Attention!</h4>
        @TempData[Alerts.WARNING]
    </div>
}
@foreach (string key in Alerts.ALL.Except(new[] { Alerts.WARNING }))
{
    if (TempData.ContainsKey(key))
    {

        <div class="toast toast-top-full-width toast-key">
            <button type="button" class="toast-close-button" data-dismiss="alert">x</button>
            @TempData[key]
        </div>

    }
}

After all this I can send alert messages right from controller: 毕竟,我可以直接从控制器发送警报消息:

And it works! 而且有效!

For example 例如

public ActionResult Test()
{
   Success("Person was successfully added to your addressbook");
}

above code would result this in view 上面的代码将导致这种情况 在此处输入图片说明

but it is just displaying as content block. 但它只是显示为内容块。 Not as I expected to work, as it just appears in view, no effect, nothing. 正如我所期望的那样,它并不像预期的那样起作用,没有效果,没有任何效果。 I wanted it to work as in this site Toastr . 我希望它像本网站Toastr一样工作

I'm guessing I have to implement javascript somewhere in my _alert view and get the message and type(success, error,...) and then use javascript to growl it, to make it behave it as it should. 我猜想我必须在_alert视图中的某处实现javascript并获取消息和类型(成功,错误等),然后使用javascript咆哮它,使其表现出应有的行为。 But i don't have much knowledge about it. 但是我对此并不了解。

Something like below? 像下面的东西? Thats just idea, because of my lack of knowledge of Javascript and jquery i couldn't make it work 那只是个想法,由于我缺乏对Javascript和jquery的了解,我无法使其正常运行

 @*if (TempData.ContainsKey(key))
    {
        <div class="toastMessageHolder" style="display: none">
            <div class="toastMessage">@TempData[key]</div>
            <div class="toastMessageType">@key</div>
        </div>



    if($(".toastMessageHolder"))
        {
        //loop all toastMessageHolders
        $(".toastMessageHolder").foreach(function(){
            var message = $(".toastMessage", this).html();
            var messageType = $(".toastMessageType", this).html();
        });

            //feed this parameters to javascript 
        }*@

Could somebody help me how to make my growling from controller behave as mentioned in example of Toastr ? 有人可以帮助我如何使我从控制器发出的咆哮像Toastr示例中提到的那样吗?

If I had to growl from any normal view if I hadn't implemented sending msgs from controller to view this is how I would do using toastr: 如果我必须从任何正常的角度抱怨,如果我没有实现从控制器发送消息的方式来查看,这就是我使用toastr的方法:

function foo(response) {
        if (response.SomeTest) {
            toastr.error(response.ErrorMessage, "Error");
        }
        else {
            $(@Html.IdFor(m=>m.abc)).val('');

        }

    };

Asp.Net MVC version: 5.1.1 Asp.Net MVC版本:5.1.1

Growling package used: Toastr 使用过的包装:烤面包机

This is what I ended up doing. 这就是我最终要做的。

_alert view(partial). _alert视图(局部)。 To send alert messages right from controller. 直接从控制器发送警报消息。

@using BootstrapSupport

<script>

        toastr.options = {
            closeButton: true,
            debug: false,
            positionClass: "toast-top-full-width",
            onclick: null,
            showDuration: "300",
            hideDuration: "1000",
            timeOut: "5000",
            extendedTimeOut: "1000",
            showEasing: "swing",
            hideEasing: "linear",
            showMethod: "fadeIn",
            hideMethod: "fadeOut"
        }

</script>

@if (TempData.ContainsKey(Alerts.SUCCESS))
{
    foreach (var value in TempData.Values)
    {
        <script>    
            toastr.success("@value.ToString()");
        </script>

    }
    TempData.Remove(Alerts.SUCCESS);

}

@if (TempData.ContainsKey(Alerts.ERROR))
{
    foreach (var value in TempData.Values)
    {
        <script>
            toastr.error("@value.ToString()");
        </script>
    }
    TempData.Remove(Alerts.ERROR);
}

@if (TempData.ContainsKey(Alerts.INFORMATION))
{
    foreach (var value in TempData.Values)
    {      
        <script>
            toastr.warning("@value.ToString()");
        </script>
    }
    TempData.Remove(Alerts.INFORMATION);
}

@if (TempData.ContainsKey(Alerts.WARNING))
{
    foreach (var value in TempData.Values)
    {
        <script>    
            toastr.warning("@value.ToString()");
        </script>
    }
    TempData.Remove(Alerts.WARNING);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM