예제 #1
0
        static public MvcHtmlString DropDownListBuilder <TModel, TProperty>(this HtmlHelper <TModel> helper,
                                                                            Expression <Func <TModel, TProperty> > expression, IEnumerable <SelectListItem> selectList, string labelText, bool optionalBlank = false)
        {
            var div = new TagBuilder("div");

            div.MergeAttribute("class", "form-group");

            if (optionalBlank)
            {
                var xBlank = new SelectListItem()
                {
                    Value = string.Empty, Text = string.Empty
                };
                selectList = xBlank.ToEnumerable().Concat(selectList).OrderBy(o => o.Value);
            }

            var label = helper.LabelFor(expression, labelText).ToHtmlString();
            var input = helper.DropDownListFor(expression, selectList, new { @class = "form-control" }).ToHtmlString();

            return(MvcHtmlString.Create(div.ToString(TagRenderMode.StartTag) + label + input + div.ToString(TagRenderMode.EndTag)));
        }