Example #1
0
        static public void SelectMultiple(this HtmlHelper html, SelectMultipleOption option)
        {
            if (option.DestinationList == null)
            {
                option.DestinationList = new SelectList(new List <SelectListItem>());
            }

            if (option.SourceList == null)
            {
                option.SourceList = new SelectList(new List <SelectListItem>());
            }

            //exclude item of source that exist in destination
            option.SourceList = new SelectList(option.SourceList.Where(pt => !option.DestinationList
                                                                       .Any(dest => pt.Value == dest.Value)), "Value", "Text");

            string destinationList1 = html.ListBox(option.DestinationListID, option.DestinationList, option.HtmlAttributes);
            string sourceList1      = html.ListBox(option.SourceListID, option.SourceList, option.HtmlAttributes);

            string htmlListBox = @"
      <table>
          <tr>
            <td>{SourceText}</td>
            <td>&nbsp;</td>
            <td>{DestinationText}</td>
          </tr>
          <tr>
            <td>{SourceList}</td>
            <td>
              <input type=""button"" id=""{AddButtonID}"" value=""►""/>
              <br/>
              <input type=""button"" id=""{DelButtonID}"" value=""◄""/>
            </td>
            <td>{DestinationList}</td>
          </tr>
      </table>
       <script type=""text/javascript"">
       $(document).ready(function(){
        $(""#{AddButtonID}"").click(function()
        {
          SelectListBoxItems(""{SourceListID}"",""{DestinationListID}"");
        });
        $(""#{DelButtonID}"").click(function()
        {
          UnSelectListBox(""{SourceListID}"",""{DestinationListID}"");
        });
      });
      </script>";

            htmlListBox = htmlListBox.Replace("{SourceList}", sourceList1);
            htmlListBox = htmlListBox.Replace("{AddButtonID}", option.AddButtonID);
            htmlListBox = htmlListBox.Replace("{DelButtonID}", option.DelButtonID);
            htmlListBox = htmlListBox.Replace("{DestinationList}", destinationList1);
            htmlListBox = htmlListBox.Replace("{SourceListID}", option.SourceListID);
            htmlListBox = htmlListBox.Replace("{DestinationListID}", option.DestinationListID);
            htmlListBox = htmlListBox.Replace("{SourceText}", option.SourceText);
            htmlListBox = htmlListBox.Replace("{DestinationText}", option.DestinationText);

            html.ViewContext.HttpContext.Response.Write(htmlListBox);
        }
        public static MvcHtmlString MyListBox(this HtmlHelper helper, string name, IEnumerable <string> selectedValue, EnumClass.EnumListType enmListType, object htmlAttributes = null, string noSelection = "")
        {
            SelectListItem blankItem = new SelectListItem {
                Selected = true, Value = "-1", Text = string.Format("-- {0} --", noSelection)
            };
            List <SelectListItem> selectedList = new List <SelectListItem>();

            if (string.IsNullOrEmpty(name))
            {
                selectedList.Add(blankItem);
                return(helper.ListBox(name, selectedList, htmlAttributes));
            }

            CacheService    _cacheservice = new CacheService();
            List <tblLists> list          = _cacheservice.GetLookupList().Where(x => x.ListType == enmListType.ToString()).ToList();

            IEnumerable <SelectListItem> items = from a in list
                                                 select new SelectListItem
            {
                Text     = string.Format("{0}-{1}", a.Id, a.ListDescription),
                Value    = a.Id.ToString(),
                Selected = selectedValue != null && selectedValue.Contains(a.Id.ToString())
            };

            return(helper.ListBox(name, items, htmlAttributes));
        }
Example #3
0
        public void ListBoxWithNonStringExplicitValue()
        {
            // Arrange
            HtmlHelper helper = HtmlHelperFactory.Create();

            // Act
            var html = helper.ListBox(
                "foo",
                null,
                GetSelectList(),
                new List <int>(),
                new { attr = "attr-val" }
                );

            // Assert
            Assert.Equal(
                "<select attr=\"attr-val\" id=\"foo\" name=\"foo\">"
                + Environment.NewLine
                + "<option value=\"A\">Alpha</option>"
                + Environment.NewLine
                + "<option value=\"B\">Bravo</option>"
                + Environment.NewLine
                + "<option value=\"C\">Charlie</option>"
                + Environment.NewLine
                + "</select>",
                html.ToHtmlString()
                );
        }
Example #4
0
        public void ListBoxWithMultiSelectAndExplitSelectValue()
        {
            // Arrange
            HtmlHelper helper     = HtmlHelperFactory.Create();
            var        selectList = GetSelectList().ToList();

            selectList.First().Selected = selectList.Last().Selected = true;

            // Act
            var html = helper.ListBox("foo", selectList, new[] { "B" }, 4, true);

            // Assert
            Assert.Equal(
                "<select id=\"foo\" multiple=\"multiple\" name=\"foo\" size=\"4\">"
                + Environment.NewLine
                + "<option selected=\"selected\" value=\"A\">Alpha</option>"
                + Environment.NewLine
                + "<option selected=\"selected\" value=\"B\">Bravo</option>"
                + Environment.NewLine
                + "<option selected=\"selected\" value=\"C\">Charlie</option>"
                + Environment.NewLine
                + "</select>",
                html.ToHtmlString()
                );
        }
Example #5
0
        public void ListBoxWithExplictAndModelValue()
        {
            // Arrange
            var modelState = new ModelStateDictionary();

            modelState.SetModelValue("foo", "C");
            HtmlHelper helper = HtmlHelperFactory.Create(modelState);

            // Act
            var html = helper.ListBox(
                "foo",
                defaultOption: null,
                selectList: GetSelectList(),
                selectedValues: "B",
                htmlAttributes: new { attr = "attr-val" }
                );

            // Assert
            Assert.Equal(
                "<select attr=\"attr-val\" id=\"foo\" name=\"foo\">"
                + Environment.NewLine
                + "<option value=\"A\">Alpha</option>"
                + Environment.NewLine
                + "<option selected=\"selected\" value=\"B\">Bravo</option>"
                + Environment.NewLine
                + "<option value=\"C\">Charlie</option>"
                + Environment.NewLine
                + "</select>",
                html.ToHtmlString()
                );
        }
Example #6
0
        public void ListBoxWithMultiSelectAndMultipleModelValue()
        {
            // Arrange
            var modelState = new ModelStateDictionary();

            modelState.SetModelValue("foo", new[] { "A", "C" });
            HtmlHelper helper = HtmlHelperFactory.Create(modelState);

            // Act
            var html = helper.ListBox("foo", GetSelectList(), null, 4, true);

            // Assert
            Assert.Equal(
                "<select id=\"foo\" multiple=\"multiple\" name=\"foo\" size=\"4\">"
                + Environment.NewLine
                + "<option selected=\"selected\" value=\"A\">Alpha</option>"
                + Environment.NewLine
                + "<option value=\"B\">Bravo</option>"
                + Environment.NewLine
                + "<option selected=\"selected\" value=\"C\">Charlie</option>"
                + Environment.NewLine
                + "</select>",
                html.ToHtmlString()
                );
        }
Example #7
0
        public void ListBoxWithModelValueAndExplicitSelectItem()
        {
            // Arrange
            var modelState = new ModelStateDictionary();

            modelState.SetModelValue("foo", new[] { "C", "D" });
            HtmlHelper helper     = HtmlHelperFactory.Create(modelState);
            var        selectList = GetSelectList().ToList();

            selectList[1].Selected = true;

            // Act
            var html = helper.ListBox("foo", selectList, new { attr = "attr-val" });

            // Assert
            Assert.Equal(
                "<select attr=\"attr-val\" id=\"foo\" name=\"foo\">"
                + Environment.NewLine
                + "<option value=\"A\">Alpha</option>"
                + Environment.NewLine
                + "<option selected=\"selected\" value=\"B\">Bravo</option>"
                + Environment.NewLine
                + "<option value=\"C\">Charlie</option>"
                + Environment.NewLine
                + "</select>",
                html.ToHtmlString()
                );
        }
Example #8
0
        public void ListBoxWithExplicitMultipleValuesAndNoMultiple()
        {
            // Arrange
            HtmlHelper helper = HtmlHelperFactory.Create();

            // Act
            var html = helper.ListBox(
                "foo",
                null,
                GetSelectList(),
                new[] { "B", "C" },
                new Dictionary <string, object> {
                { "attr", "attr-val" }
            }
                );

            // Assert
            Assert.Equal(
                "<select attr=\"attr-val\" id=\"foo\" name=\"foo\">"
                + Environment.NewLine
                + "<option value=\"A\">Alpha</option>"
                + Environment.NewLine
                + "<option selected=\"selected\" value=\"B\">Bravo</option>"
                + Environment.NewLine
                + "<option value=\"C\">Charlie</option>"
                + Environment.NewLine
                + "</select>",
                html.ToHtmlString()
                );
        }
Example #9
0
        public void ListBoxWithErrorAndExplictAndModelState()
        {
            // Arrange
            var modelState = new ModelStateDictionary();

            modelState.SetModelValue("foo", "C");
            modelState.AddError("foo", "test");
            HtmlHelper helper = HtmlHelperFactory.Create(modelState);

            // Act
            var html = helper.ListBox("foo.bar", "Select One", GetSelectList());

            // Assert
            Assert.Equal(
                "<select id=\"foo_bar\" name=\"foo.bar\">"
                + Environment.NewLine
                + "<option value=\"\">Select One</option>"
                + Environment.NewLine
                + "<option value=\"A\">Alpha</option>"
                + Environment.NewLine
                + "<option value=\"B\">Bravo</option>"
                + Environment.NewLine
                + "<option value=\"C\">Charlie</option>"
                + Environment.NewLine
                + "</select>",
                html.ToHtmlString()
                );
        }
Example #10
0
        public void ListBoxWithEmptyOptionLabel()
        {
            // Arrange
            var modelState = new ModelStateDictionary();

            modelState.AddError("foo", "some error");
            HtmlHelper helper = HtmlHelperFactory.Create(modelState);

            // Act
            var html = helper.ListBox("foo", GetSelectList(), new { @class = "my-class" });

            // Assert
            Assert.Equal(
                "<select class=\"input-validation-error my-class\" id=\"foo\" name=\"foo\">"
                + Environment.NewLine
                + "<option value=\"A\">Alpha</option>"
                + Environment.NewLine
                + "<option value=\"B\">Bravo</option>"
                + Environment.NewLine
                + "<option value=\"C\">Charlie</option>"
                + Environment.NewLine
                + "</select>",
                html.ToHtmlString()
                );
        }
Example #11
0
        /// <summary>实体列表的下拉列表。多选,自动匹配当前模型的选中项,支持数组类型或字符串类型(自动分割)的选中项</summary>
        /// <param name="Html"></param>
        /// <param name="name"></param>
        /// <param name="list"></param>
        /// <param name="autoPostback">自动回发</param>
        /// <returns></returns>
        public static MvcHtmlString ForListBox(this HtmlHelper Html, String name, IList <IEntity> list, Boolean autoPostback = false)
        {
            var entity = Html.ViewData.Model as IEntity;
            var vs     = entity == null ? WebHelper.Params[name] : entity[name];

            // 如果是字符串,分割为整型数组,全局约定逗号分割
            if (vs is String)
            {
                vs = (vs as String).SplitAsInt();
            }

            var atts = new RouteValueDictionary();

            if (Setting.Current.BootstrapSelect)
            {
                atts.Add("class", "multiselect");
            }
            else
            {
                atts.Add("class", "form-control");
            }
            atts.Add("multiple", "");
            // 处理自动回发
            if (autoPostback)
            {
                atts.Add("onchange", "$(':submit').click();");
            }

            return(Html.ListBox(name, new MultiSelectList(list.ToDictionary(), "Key", "Value", vs as IEnumerable), atts));
        }
Example #12
0
        public static MvcHtmlString ListBox(this AjaxHelper ajaxHelper,
                                            string name,
                                            IEnumerable <SelectListItem> selectList = null,
                                            string cssClass = null,
                                            string dir      = null,
                                            bool disabled   = false,
                                            string id       = null,
                                            string lang     = null,
                                            int?size        = null,
                                            string style    = null,
                                            int?tabIndex    = null,
                                            string title    = null,
                                            string dataBind = null)
        {
            HtmlHelper htmlHelper = ajaxHelper.GetHtmlHelper();

            if (dataBind == null)
            {
                dataBind = "value: " + name;
            }
            return(htmlHelper.ListBox(
                       name,
                       selectList,
                       SelectAttributes(cssClass, dir, disabled, id, lang, size, style, tabIndex, title, dataBind)));
        }
Example #13
0
        /// <summary>字典的下拉列表1</summary>
        /// <param name="Html"></param>
        /// <param name="name"></param>
        /// <param name="dic"></param>
        /// <param name="selectedValues"></param>
        /// <param name="autoPostback">自动回发</param>
        /// <returns></returns>
        public static MvcHtmlString ForListBoxX(this HtmlHelper Html, String name, IDictionary dic, IEnumerable selectedValues, Boolean autoPostback = false, Object htmlAttributes = null)
        {
            var atts = htmlAttributes.ToAttrDictionary();

            if (NewLife.Cube.Setting.Current.BootstrapSelect)
            {
                TryAddAttribute(atts, "class", "multiselect");
            }
            else
            {
                TryAddAttribute(atts, "class", "multiselect");
            }

            //atts.Add("multiple", "");
            TryAddAttribute(atts, "multiple", "");
            // 处理自动回发
            //if (autoPostback) atts.Add("onchange", "$(':submit').click();");

            if (autoPostback)
            {
                TryAddAttribute(atts, "onchange", "$(this).parents('form').submit();");
            }

            return(Html.ListBox(name, new MultiSelectList(dic, "Key", "Value", selectedValues), atts));
        }
Example #14
0
    public static MvcHtmlString AcknowledgeListBox <TModel, TProperty>(this HtmlHelper <TModel> helper, string name, IEnumerable <SelectListItem> selectList, object containerHtmlAttributes = null, object inputHtmlAttributes = null, string dataType = "text")
    {
        IDictionary <string, object> containerAttribs = HtmlHelper.AnonymousObjectToHtmlAttributes(containerHtmlAttributes);

        if (containerAttribs == null)
        {
            containerAttribs = new Dictionary <string, object>();
        }

        IDictionary <string, object> inputAttribs = HtmlHelper.AnonymousObjectToHtmlAttributes(inputHtmlAttributes);

        if (inputAttribs == null)
        {
            inputAttribs = new Dictionary <string, object>();
        }
        inputAttribs.Add("data-type", dataType);

        TagBuilder containerBuilder = new TagBuilder("div");

        containerBuilder.MergeAttributes(containerAttribs);
        containerBuilder.Attributes.Add("data-role", "acknowledge-input");
        containerBuilder.AddCssClass("input-append");
        containerBuilder.InnerHtml = helper.ListBox(name, selectList, inputAttribs).ToHtmlString() + "<div data-role='acknowledgement'><i></i></div>";

        return(new MvcHtmlString(containerBuilder.ToString()));
    }
Example #15
0
        /// <summary>实体列表的下拉列表。多选,自动匹配当前模型的选中项,支持数组类型或字符串类型(自动分割)的选中项2</summary>
        /// <param name="Html"></param>
        /// <param name="name"></param>
        /// <param name="list"></param>
        /// <param name="autoPostback">自动回发</param>
        /// <returns></returns>
        public static MvcHtmlString ForListBoxX(this HtmlHelper Html, String name, IList <IEntity> list, Boolean autoPostback = false, Object htmlAttributes = null)
        {
            var entity = Html.ViewData.Model as IEntity;
            var vs     = entity == null ? WebHelper.Params[name] : entity[name];

            // 如果是字符串,分割为整型数组,全局约定逗号分割
            if (vs is String)
            {
                vs = (vs as String).SplitAsInt();
            }

            var atts = htmlAttributes.ToAttrDictionary();

            if (NewLife.Cube.Setting.Current.BootstrapSelect)
            {
                TryAddAttribute(atts, "class", "multiselect");
            }
            else
            {
                TryAddAttribute(atts, "class", "multiselect");
            }
            //atts.Add("multiple", "");
            TryAddAttribute(atts, "multiple", "");
            // 处理自动回发
            //if (autoPostback) atts.Add("onchange", "$(':submit').click();");
            if (autoPostback)
            {
                TryAddAttribute(atts, "onchange", "$(this).parents('form').submit();");
            }

            return(Html.ListBox(name, new MultiSelectList(list.ToDictionary(), "Key", "Value", vs as IEnumerable), atts));
        }
 public static MvcHtmlString ListBox(this HtmlHelper htmlHelper, string name, IEnumerable <SelectListItem> selectList = null, string cssClass = null, string dir = null, bool disabled = false, string id = null, string lang = null, int?size = null, string style = null, int?tabIndex = null, string title = null)
 {
     return(htmlHelper.ListBox(
                name,
                selectList,
                SelectAttributes(cssClass, dir, disabled, id, lang, size, style, tabIndex, title)));
 }
Example #17
0
        /// <summary>Generate a control for a set preference.</summary>
        private static string SetPreferenceControl(HtmlHelper helper, MetaAttribute ma, Preference preference)
        {
            string result;

            if (ma.HasChoices)
            {
                if (preference != null)
                {
                    MultiSelectList listData = new MultiSelectList(SelectHelper.DropDownRecordsFromValueSet(ma.ChoicesCollection), "Value", "Text",
                        SelectHelper.ValuesFromValueSet(preference.Set));
                    result = helper.ListBox(ma.PreferenceSetControlName, listData);
                }
                else
                {
                    MultiSelectList listData = new MultiSelectList(SelectHelper.DropDownRecordsFromValueSet(ma.ChoicesCollection), "Value", "Text");
                    result = helper.ListBox(ma.PreferenceSetControlName, listData);
                }
            }
            else
            {
                if (preference != null)
                {
                    result = helper.TextBox(ma.PreferenceSetControlName, preference.RawValues);
                }
                else
                {
                    result = helper.TextBox(ma.PreferenceSetControlName);
                }
            }
            return result;
        }
Example #18
0
        public void ListBoxWithObjectDictionaryAndTitle()
        {
            // Arrange
            HtmlHelper helper = HtmlHelperFactory.Create();

            // Act
            var html = helper.ListBox(
                "foo",
                "Select One",
                GetSelectList(),
                new { @class = "my-class" }
                );

            // Assert
            Assert.Equal(
                "<select class=\"my-class\" id=\"foo\" name=\"foo\">"
                + Environment.NewLine
                + "<option value=\"\">Select One</option>"
                + Environment.NewLine
                + "<option value=\"A\">Alpha</option>"
                + Environment.NewLine
                + "<option value=\"B\">Bravo</option>"
                + Environment.NewLine
                + "<option value=\"C\">Charlie</option>"
                + Environment.NewLine
                + "</select>",
                html.ToHtmlString()
                );
        }
Example #19
0
        MvcHtmlString ICustomFieldRender <SourceMultipleFieldType> .RenderHtmlEditor <TModel>(HtmlHelper <TModel> html, IField field, IDictionary <string, object> htmlAttributes, params object[] additionalParameters)
        {
            var value  = (field as FieldData)?.Value;
            var values = (value as IEnumerable <int>)?.Select(x => x);

            if (htmlAttributes == null)
            {
                htmlAttributes = new Dictionary <string, object>();
            }
            if (!string.IsNullOrEmpty(field.alias))
            {
                htmlAttributes["class"] = (htmlAttributes.GetValueOrDefault("class", null) ?? "") + " FieldAlias_" + field.alias;
            }

            htmlAttributes = htmlAttributes.Where(x => x.Key.ToLower() != "size").ToDictionary(x => x.Key, x => x.Value);
            //htmlAttributes["multiple"] = true;

            var list = (field.data != null ? field.data.Select(x => new SelectListItem()
            {
                Value = x.IdFieldValue.ToString(),
                Text = x.FieldValue,
                Selected = values != null && values.Contains(x.IdFieldValue)
            }) : Enumerable.Empty <SelectListItem>()).ToList();

            return(html.ListBox($"fieldValue_{field.IdField}[]", list, htmlAttributes));
        }
Example #20
0
        public void ListBoxThrowsWithNoName()
        {
            // Arrange
            HtmlHelper helper = new HtmlHelper(new ModelStateDictionary());

            // Act and assert
            ExceptionAssert.ThrowsArgNullOrEmpty(() => helper.ListBox(name: null, selectList: null), "name");
        }
Example #21
0
        public void ListBoxThrowsWithNoName()
        {
            // Arrange
            HtmlHelper helper = HtmlHelperFactory.Create();

            // Act and assert
            Assert.ThrowsArgumentNullOrEmptyString(() => helper.ListBox(name: null, selectList: null), "name");
        }
        public override MvcHtmlString WriteInput(HtmlHelper helper, object htmlAttributes)
        {
            IDictionary<string, object> efHtmlAttributes = new RouteValueDictionary(htmlAttributes);
            AddCommonHtmlAttributes(efHtmlAttributes);

            // This is an ugly hack.  For some reason, when inputName is used here, the Select attributes don't render and the selection is lost.
            // See here for possible explanation: http://stackoverflow.com/questions/3737985/asp-net-mvc-multiselectlist-with-selected-values-not-selecting-properly
            return helper.ListBox(this.InputName + "01", this.SelectListItems, efHtmlAttributes);
        }
        public override MvcHtmlString WriteInput(HtmlHelper helper, object htmlAttributes)
        {
            IDictionary <string, object> efHtmlAttributes = new RouteValueDictionary(htmlAttributes);

            AddCommonHtmlAttributes(efHtmlAttributes);

            // This is an ugly hack.  For some reason, when inputName is used here, the Select attributes don't render and the selection is lost.
            // See here for possible explanation: http://stackoverflow.com/questions/3737985/asp-net-mvc-multiselectlist-with-selected-values-not-selecting-properly
            return(helper.ListBox(this.InputName + "01", this.SelectListItems, efHtmlAttributes));
        }
Example #24
0
        public void SelectHelpersUseCurrentCultureToConvertValues()
        {
            // Arrange
            HtmlHelper defaultValueHelper = HtmlHelperTest.GetHtmlHelper(new ViewDataDictionary {
                { "foo", new [] { new DateTime(1900, 1, 1, 0, 0, 1) } },
                { "bar", new DateTime(1900, 1, 1, 0, 0, 1) }
            });
            HtmlHelper helper     = HtmlHelperTest.GetHtmlHelper();
            SelectList selectList = new SelectList(GetSampleCultureAnonymousObjects(), "Date", "FullWord", new DateTime(1900, 1, 1, 0, 0, 0));

            var tests = new[] {
                // DropDownList(name, selectList, optionLabel)
                new {
                    Html   = @"<select id=""foo"" name=""foo""><option selected=""selected"" value=""1900/01/01 12:00:00 AM"">Alpha</option>
<option value=""1900/01/01 12:00:01 AM"">Bravo</option>
<option value=""1900/01/01 12:00:02 AM"">Charlie</option>
</select>",
                    Action = new GenericDelegate <string>(() => helper.DropDownList("foo", selectList, (string)null))
                },
                // DropDownList(name, selectList, optionLabel) (With default value selected from ViewData)
                new {
                    Html   = @"<select id=""bar"" name=""bar""><option value=""1900/01/01 12:00:00 AM"">Alpha</option>
<option selected=""selected"" value=""1900/01/01 12:00:01 AM"">Bravo</option>
<option value=""1900/01/01 12:00:02 AM"">Charlie</option>
</select>",
                    Action = new GenericDelegate <string>(() => defaultValueHelper.DropDownList("bar", selectList, (string)null))
                },

                // ListBox(name, selectList)
                new {
                    Html   = @"<select id=""foo"" multiple=""multiple"" name=""foo""><option selected=""selected"" value=""1900/01/01 12:00:00 AM"">Alpha</option>
<option value=""1900/01/01 12:00:01 AM"">Bravo</option>
<option value=""1900/01/01 12:00:02 AM"">Charlie</option>
</select>",
                    Action = new GenericDelegate <string>(() => helper.ListBox("foo", selectList))
                },

                // ListBox(name, selectList) (With default value selected from ViewData)
                new {
                    Html   = @"<select id=""foo"" multiple=""multiple"" name=""foo""><option value=""1900/01/01 12:00:00 AM"">Alpha</option>
<option selected=""selected"" value=""1900/01/01 12:00:01 AM"">Bravo</option>
<option value=""1900/01/01 12:00:02 AM"">Charlie</option>
</select>",
                    Action = new GenericDelegate <string>(() => defaultValueHelper.ListBox("foo", selectList))
                }
            };

            // Act && Assert
            using (HtmlHelperTest.ReplaceCulture("en-ZA", "en-US")) {
                foreach (var test in tests)
                {
                    Assert.AreEqual(test.Html, test.Action());
                }
            }
        }
Example #25
0
        public void ListBoxWithNullNameThrows()
        {
            // Arrange
            HtmlHelper helper = HtmlHelperTest.GetHtmlHelper(new ViewDataDictionary());

            // Act & Assert
            ExceptionHelper.ExpectArgumentExceptionNullOrEmpty(
                delegate {
                helper.ListBox(null /* name */, (MultiSelectList)null /* selectList */);
            },
                "name");
        }
Example #26
0
        /// <summary>字典的下拉列表</summary>
        /// <param name="Html"></param>
        /// <param name="name"></param>
        /// <param name="dic"></param>
        /// <param name="selectedValues"></param>
        /// <param name="autoPostback">自动回发</param>
        /// <returns></returns>
        public static MvcHtmlString ForListBox(this HtmlHelper Html, String name, IDictionary dic, IEnumerable selectedValues, Boolean autoPostback = false)
        {
            var atts = new RouteValueDictionary();

            atts.Add("class", "multiselect");
            atts.Add("multiple", "");
            // 处理自动回发
            if (autoPostback)
            {
                atts.Add("onchange", "$(':submit').click();");
            }

            return(Html.ListBox(name, new MultiSelectList(dic, "Key", "Value", selectedValues), atts));
        }
Example #27
0
        public static string DisplayMemberEditableMultipleValues(this HtmlHelper helper, ModelInstance.MemberMultipleValues member, string label)
        {
            StringBuilder returnStr = new StringBuilder();

            AppendLabel(label, returnStr);
            IEnumerable <SelectListItem> listItens = member.AllowedValues.Select((v, i) => new SelectListItem()
            {
                Text     = member.AllowedVisibleValues[i],
                Value    = v,
                Selected = member.Values.Contains(v)
            });

            return(returnStr.Append(helper.ListBox(member.Name, listItens, new { multiple = "multiple" })).ToString());
        }
Example #28
0
        public static MvcHtmlString ListBox(this HtmlHelper htmlHelper, string name, string listName, IEnumerable <string> values)
        {
            var listItems = ListProviders.Current.GetListItems(listName);
            List <SelectListItem> selectListItems = new List <SelectListItem>();

            foreach (var item in listItems)
            {
                selectListItems.Add(new SelectListItem()
                {
                    Value = item.Value, Text = item.Text, Selected = values.Any(value => value == item.Value)
                });
            }

            return(htmlHelper.ListBox(name, selectListItems));
        }
        /// <summary>实体列表的下拉列表。多选</summary>
        /// <param name="Html"></param>
        /// <param name="name"></param>
        /// <param name="list"></param>
        /// <param name="selectedValues">已选择项</param>
        /// <returns></returns>
        public static MvcHtmlString ForListBox <TEntity>(this HtmlHelper Html, String name, IList <TEntity> list, String selectedValues) where TEntity : IEntity
        {
            var atts = new RouteValueDictionary();

            if (Setting.Current.BootstrapSelect)
            {
                atts.Add("class", "multiselect");
            }
            else
            {
                atts.Add("class", "form-control");
            }
            atts.Add("multiple", "");

            return(Html.ListBox(name, new MultiSelectList(list.ToDictionary(), "Key", "Value", selectedValues.Split(",")), atts));
        }
        public static MvcHtmlString EnumMultipleList <TModel>(this HtmlHelper <TModel> htmlHelper, ModelMetadata metadata, string name, object htmlAttributes)
        {
            Type enumType             = metadata.ModelType.GetElementType();
            IEnumerable <Enum> values = Enum.GetValues(enumType).Cast <Enum>();


            IEnumerable <SelectListItem> items = from value in values
                                                 select new SelectListItem
            {
                Text     = GetEnumDescription(value),
                Value    = value.ToString(),
                Selected = (metadata.Model == null ? false : Array.IndexOf((Array)metadata.Model, value) != -1)
            };

            return(htmlHelper.ListBox(name, items, htmlAttributes));
        }
Example #31
0
        public void ListBoxWithNonStringExplicitValue()
        {
            // Arrange
            HtmlHelper helper = HtmlHelperFactory.Create();

            // Act
            var html = helper.ListBox("foo", null, GetSelectList(), new List <int>(), new { attr = "attr-val" });

            // Assert
            Assert.Equal(
                @"<select attr=""attr-val"" id=""foo"" name=""foo"">
<option value=""A"">Alpha</option>
<option value=""B"">Bravo</option>
<option value=""C"">Charlie</option>
</select>",
                html.ToHtmlString());
        }
Example #32
0
        public void ListBoxWithMultiSelectAndMultipleExplicitValues()
        {
            // Arrange
            HtmlHelper helper = HtmlHelperFactory.Create();

            // Act
            var html = helper.ListBox("foo", GetSelectList(), new[] { "A", "C" }, 4, true);

            // Assert
            Assert.Equal(
                @"<select id=""foo"" multiple=""multiple"" name=""foo"" size=""4"">
<option selected=""selected"" value=""A"">Alpha</option>
<option value=""B"">Bravo</option>
<option selected=""selected"" value=""C"">Charlie</option>
</select>",
                html.ToHtmlString());
        }
Example #33
0
        private static MvcHtmlString ListBoxForParameter(HtmlHelper htmlHelper, ReportParameter reportParameter, bool disabled)
        {
            Dictionary<string, object> htmlAttributes = GetHtmlAttributes(reportParameter.Dependents, null, disabled);

            return htmlHelper.ListBox(reportParameter.Id, reportParameter.Options, htmlAttributes);
        }
        public static string RenderSelectElement(HtmlHelper html, BootstrapSelectElementModel model, BootstrapInputType inputType)
        {
            if (model == null || string.IsNullOrEmpty(model.htmlFieldName) || model.selectList == null) return null;

            string combinedHtml = "{0}{1}{2}";
            if (model.selectedValue != null)
            {
                foreach (var item in model.selectList)
                {
                    if (item.Value == model.selectedValue.ToString())
                        item.Selected = true;
                }
            }

            model.htmlAttributes.AddRange(html.GetUnobtrusiveValidationAttributes(model.htmlFieldName, model.metadata));
            if (model.tooltipConfiguration != null) model.htmlAttributes.AddRange(model.tooltipConfiguration.ToDictionary());
            if (!string.IsNullOrEmpty(model.id)) model.htmlAttributes.AddOrReplace("id", model.id);

            // assign size class
            model.htmlAttributes.AddOrMergeCssClass("class", BootstrapHelper.GetClassForInputSize(model.size));

            // build html for input
            string input = string.Empty;

            if(inputType == BootstrapInputType.DropDownList)
                input = html.DropDownList(model.htmlFieldName, model.selectList, model.optionLabel, model.htmlAttributes.FormatHtmlAttributes()).ToHtmlString();

            if(inputType == BootstrapInputType.ListBox)
                input = html.ListBox(model.htmlFieldName, model.selectList, model.htmlAttributes.FormatHtmlAttributes()).ToHtmlString();

            // account for appendString, prependString, and AppendButtons
            TagBuilder appendPrependContainer = new TagBuilder("div");
            if (!string.IsNullOrEmpty(model.prependString) | !string.IsNullOrEmpty(model.appendString) | model.appendButtons.Count() > 0)
            {
                string addOnPrependString = "";
                string addOnAppendString = "";
                string addOnPrependButtons = "";
                string addOnAppendButtons = "";

                TagBuilder addOn = new TagBuilder("span");
                addOn.AddCssClass("add-on");
                if (!string.IsNullOrEmpty(model.prependString))
                {
                    appendPrependContainer.AddOrMergeCssClass("input-prepend");
                    addOn.InnerHtml = model.prependString;
                    addOnPrependString = addOn.ToString();
                }
                if (!string.IsNullOrEmpty(model.appendString))
                {
                    appendPrependContainer.AddOrMergeCssClass("input-append");
                    addOn.InnerHtml = model.appendString;
                    addOnAppendString = addOn.ToString();
                }
                if (model.appendButtons.Count() > 0)
                {
                    appendPrependContainer.AddOrMergeCssClass("input-append");
                    ((List<BootstrapButton>)model.appendButtons).ForEach(x => addOnAppendButtons += x.ToHtmlString());
                }
                if (model.prependButtons.Count() > 0)
                {
                    appendPrependContainer.AddOrMergeCssClass("input-append");
                    ((List<BootstrapButton>)model.prependButtons).ForEach(x => addOnPrependButtons += x.ToHtmlString());
                }

                appendPrependContainer.InnerHtml = addOnPrependButtons + addOnPrependString + "{0}" + addOnAppendString + addOnAppendButtons;
                combinedHtml = appendPrependContainer.ToString(TagRenderMode.Normal) + "{1}{2}";
            }

            string helpText = model.helpText != null ? model.helpText.ToHtmlString() : string.Empty;
            string validationMessage = "";
            if (model.displayValidationMessage)
            {
                string validation = html.ValidationMessage((string)model.htmlFieldName).ToHtmlString();
                validationMessage = new BootstrapHelpText(validation, model.validationMessageStyle).ToHtmlString();
            }

            return MvcHtmlString.Create(string.Format(combinedHtml, input, validationMessage, helpText)).ToString();
        }