예제 #1
0
        protected RouteValueDictionary GetAttributes()
        {
            bool isIml = OnInit != null ||
                         OnChange != null ||
                         OnEvent != null;

            if (isIml)
            {
                attributes.Merge(new IncodingMetaLanguageDsl(JqueryBind.InitIncoding)
                                 .OnSuccess(dsl =>
                {
                    OnInit.Do(action => action(dsl));
                    OnEvent.Do(action => action(dsl));
                })
                                 .When(JqueryBind.Change)
                                 .OnSuccess(dsl =>
                {
                    OnChange.Do(action => action(dsl));
                    OnEvent.Do(action => action(dsl));
                })
                                 .AsHtmlAttributes());
            }

            return(attributes);
        }
        public override MvcHtmlString ToHtmlString()
        {
            var  optionals = Optional.Recovery(new KeyValueVm[0]);
            bool isAjax    = !string.IsNullOrWhiteSpace(Url);

            bool isIml = OnInit != null ||
                         OnChange != null ||
                         this.OnEvent != null;

            if (isAjax || isIml)
            {
                var meta = isAjax ? this.htmlHelper.When(InitBind).Do().Ajax(options =>
                {
                    options.Url  = Url;
                    options.Type = HttpVerbs.Get;
                    if (Cache.HasValue)
                    {
                        options.Cache = Cache.Value;
                    }
                }) : this.htmlHelper.When(InitBind).Do().Direct();
                this.attributes.Merge(meta.OnSuccess(dsl =>
                {
                    if (isAjax)
                    {
                        dsl.Self().Core().Insert.WithTemplate(Template).Html();
                        foreach (var vm in optionals)
                        {
                            var option = new TagBuilder(HtmlTag.Option.ToStringLower());
                            option.SetInnerText(vm.Text);
                            option.MergeAttribute(HtmlAttribute.Value.ToStringLower(), vm.Value);
                            dsl.Self().Core().JQuery.Manipulation.Prepend(new MvcHtmlString(option.ToString()).ToHtmlString());
                        }

                        var selected = ModelMetadata.FromLambdaExpression(this.property, this.htmlHelper.ViewData).Model;
                        if (selected != null)
                        {
                            dsl.Self().Core().JQuery.Attributes.Val(selected);
                        }
                    }

                    OnInit.Do(action => action(dsl));
                    OnEvent.Do(action => action(dsl));
                })
                                      .When(JqueryBind.Change)
                                      .Do()
                                      .Direct()
                                      .OnSuccess(dsl =>
                {
                    OnChange.Do(action => action(dsl));
                    OnEvent.Do(action => action(dsl));
                })
                                      .AsHtmlAttributes());
            }

            return(!isAjax && optionals.Any()
                           ? this.htmlHelper.DropDownListFor(this.property, Data, optionals.FirstOrDefault().Text, this.attributes)
                           : this.htmlHelper.DropDownListFor(this.property, Data, this.attributes));
        }
        public override void WriteTo(TextWriter writer, HtmlEncoder encoder)
        {
            string currentUrl = Data;
            bool   isAjax     = !string.IsNullOrWhiteSpace(currentUrl);

            var meta = isAjax ? this.htmlHelper.When(InitBind).Ajax(currentUrl)
                               : this.htmlHelper.When(InitBind);

            attributes = meta.OnSuccess(dsl =>
            {
                if (isAjax)
                {
                    dsl.Self().JQuery.Dom.Empty();
                    foreach (var vm in (List <KeyValueVm>)Data.Optional)
                    {
                        var option = new TagBuilder(HtmlTag.Option.ToStringLower());
                        option.InnerHtml.Append(vm.Text);
                        option.MergeAttribute(HtmlAttribute.Value.ToStringLower(), vm.Value);
                        dsl.Self().JQuery.Dom.Use(option.ToHtmlString()).Prepend();
                    }
                    dsl.Self().Insert.WithTemplate(Template).Append();
                }

                var selected = ExpressionMetadataProvider.FromLambdaExpression(property, htmlHelper.ViewData, htmlHelper.MetadataProvider).Model;
                if (selected != null)
                {
                    dsl.Self().JQuery.Attr.Val(selected);
                }

                OnInit.Do(action => action(dsl));
                OnEvent.Do(action => action(dsl));
            })
                         .When(JqueryBind.Change)
                         .OnSuccess(dsl =>
            {
                OnChange.Do(action => action(dsl));
                OnEvent.Do(action => action(dsl));
            })
                         .AsHtmlAttributes(this.attributes);

            var tag = this.htmlHelper.DropDownListFor(this.property, isAjax ? new SelectList(new string[] { }) : (SelectList)Data, null, this.attributes);

            tag.WriteTo(writer, encoder);
        }