コード例 #1
0
        public static MvcHtmlString CultureSpecificLabel <TModel, TValue>(this HtmlHelper <TModel> html, Expression <Func <TModel, TValue> > expression, object htmlAttributes, string module = "")
        {
            string moduleName = "";

            if (module != "")
            {
                moduleName = module;
            }

            else if (html.ViewBag.ModuleName != null)
            {
                moduleName = html.ViewBag.ModuleName;
            }
            else if (html.ViewData.Model != null)
            {
                var moduelNameAttribute = (html.ViewData.Model.GetType().GetCustomAttributes(false).FirstOrDefault());

                if (moduelNameAttribute is CultureModuleAttribute)
                {
                    moduleName = ((CultureModuleAttribute)moduelNameAttribute).ModuleName;
                }
            }

            if (!string.IsNullOrEmpty(moduleName))
            {
                return(LabelExtensions.LabelFor(html, expression, CommonFunctions.GetGlobalizedLabel(moduleName, ((MemberExpression)expression.Body).Member.Name), htmlAttributes));
            }
            else
            {
                return(LabelExtensions.LabelFor(html, expression, htmlAttributes));
            }
        }
コード例 #2
0
        //摘要:
        //    返回一个 HTML label 元素以及由指定表达式表示的属性的属性名称。

        //参数:
        //  html:
        //    此方法扩展的 HTML 帮助器实例。

        //  expression:
        //    一个表达式,用于标识要显示的属性。

        //类型参数:
        //  TModel:
        //    模型的类型。

        //  TValue:
        //    值的类型。

        //返回结果:
        //    一个 HTML label 元素以及由表达式表示的属性的属性名称。
        public static MvcHtmlString KOLabelFor <TModel, TValue>(this HtmlHelper <TModel> html, Expression <Func <TModel, TValue> > expression)
        {
            var htmlAttributes = new Dictionary <string, object>();

            htmlAttributes.Add("data-bind", string.Format("text:{0}", GetMemberName(expression)));
            return(LabelExtensions.LabelFor(html, expression, htmlAttributes));
        }
コード例 #3
0
        public static MvcHtmlString LabelForRequired <TModel, TValue>(this HtmlHelper <TModel> html, Expression <Func <TModel, TValue> > expression, string labelText = null)
        {
            var  metadata   = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
            bool isRequired =
                metadata.ContainerType.GetProperty(metadata.PropertyName)
                .GetCustomAttributes(typeof(RequiredAttribute), false)
                .Any();

            string requiredMvcStr = "";

            if (isRequired)
            {
                var tagBuilder = new TagBuilder("span");
                tagBuilder.Attributes.Add("class", "required");
                tagBuilder.SetInnerText("*");
                requiredMvcStr = tagBuilder.ToString(TagRenderMode.Normal);
            }

            var labelResult = labelText == null?
                              LabelExtensions.LabelFor(html, expression) :
                                  LabelExtensions.LabelFor(html, expression, labelText);

            var result = new MvcHtmlString(string.Concat(labelResult, requiredMvcStr));

            return(result);
        }
コード例 #4
0
        public static MvcHtmlString JQM_TextBoxFor <TModel, TProperty>(this HtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TProperty> > expression, InputConfig config = null, IDictionary <string, object> htmlAttributes = null)
        {
            TagBuilder tagResult = new TagBuilder("div");

            tagResult.MergeAttribute("class", "ui-field-contain");
            if (config == null || (config != null && string.IsNullOrEmpty(config.PlaceHolder)))
            {
                tagResult.InnerHtml += LabelExtensions.LabelFor(htmlHelper, expression).ToHtmlString();
            }

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

            if (config != null)
            {
                foreach (var item in config.GetAttributes())
                {
                    if (htmlAttributes.Count(p => p.Key == item.Key) == 0)
                    {
                        htmlAttributes.Add(item);
                    }
                }
            }
            tagResult.InnerHtml += InputExtensions.TextBoxFor(htmlHelper, expression, htmlAttributes).ToHtmlString();
            tagResult.InnerHtml += ValidationExtensions.ValidationMessageFor(htmlHelper, expression).ToHtmlString();
            return(tagResult.ToHtml());
        }
コード例 #5
0
        public static string Object(HtmlHelper html)
        {
            ModelMetadata metadata = html.Context.ViewData.Metadata;
            object        model    = html.Context.ViewData.Model;
            Type          type     = html.Context.ViewData.Template.Type;

            if (model == null)
            {
                return(metadata.NullDisplayText);
            }

            if (html.Context.ViewData.Template.Depth > 1)
            {
                return(metadata.GetDisplayText(model));
            }

            StringBuilder builder = new StringBuilder();

            foreach (ModelMetadata property in metadata.Properties
                     .Where(pm => pm.Visible).OrderBy(pm => pm.FieldOrder))
            {
                object propertyValue = (model == null) ? null : property.GetModel(model);
                if (!metadata.HideSurroundingChrome)
                {
                    builder.Append(LabelExtensions.Render(html.Context, property.PropertyName, property));
                }

                builder.Append(html.Templates.Render(DataBoundControlMode.ReadOnly,
                                                     property, null, property.PropertyName, propertyValue));
            }
            return(builder.ToString());
        }
コード例 #6
0
        public static MvcHtmlString BootstrapEditorFor <TModel, TValue>(this HtmlHelper <TModel> helper, Expression <Func <TModel, TValue> > expression)
        {
            MvcHtmlString label      = LabelExtensions.LabelFor(helper, expression, new { @class = "control-label col-xs-12 col-sm-4 col-md-3" });
            MvcHtmlString editor     = EditorExtensions.EditorFor(helper, expression, new { htmlAttributes = new { @class = "form-control" } });
            MvcHtmlString validation = ValidationExtensions.ValidationMessageFor(helper, expression, null, new { @class = "text-danger" });
            // Build the input elements
            TagBuilder editorDiv = new TagBuilder("div");

            editorDiv.AddCssClass("col-xs-4 col-sm-2 col-md-2 col-lg-1");
            editorDiv.InnerHtml = editor.ToString();
            // Build the validation message elements
            TagBuilder validationSpan = new TagBuilder("span");

            validationSpan.AddCssClass("help-block");
            validationSpan.InnerHtml = validation.ToString();
            TagBuilder validationDiv = new TagBuilder("div");

            validationDiv.AddCssClass("col-xs-12 col-md-8 col-sm-offset-4 col-md-offset-3");
            validationDiv.InnerHtml = validationSpan.ToString();
            // Combine all elements
            StringBuilder html = new StringBuilder();

            html.Append(label.ToString());
            html.Append(editorDiv.ToString());
            html.Append(validationDiv.ToString());
            // Build the outer div
            TagBuilder outerDiv = new TagBuilder("div");

            outerDiv.AddCssClass("form-group");
            outerDiv.InnerHtml = html.ToString();
            return(MvcHtmlString.Create(outerDiv.ToString()));
        }
コード例 #7
0
 public static MvcHtmlString LabelFor <TModel, TValue>(
     this HtmlHelper <TModel> htmlHelper,
     Expression <Func <TModel, TValue> > expression
     )
 {
     return(LabelExtensions.LabelFor(htmlHelper, expression));
 }
コード例 #8
0
        public static MvcHtmlString BSFGForMetaData <TModel>(
            this HtmlHelper <TModel> html,
            MetaProduct meta, int num,
            int col_sm)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("<div class=\"form-group\">");
            var name = (typeof(TypeOfMetaProduct).GetMember(meta.Type.ToString()).First().GetCustomAttributes(typeof(DisplayAttribute), true).First() as DisplayAttribute).Name;

            sb.AppendLine(LabelExtensions
                          .Label(html, name, new { @class = String.Format("col-sm-{0} control-label", 12 - col_sm) }).ToHtmlString());

            sb.AppendLine(String.Format("<div class=\"col-sm-{0}\">", col_sm));
            sb.AppendLine("<div class=\"input-group\">");
            sb.AppendLine(InputExtensions
                          .Hidden(html, String
                                  .Format("MetaData[{0}].Type", num), meta.Type.ToString()).ToHtmlString());
            switch (meta.Type)
            {
            case TypeOfMetaProduct.Author:
            case TypeOfMetaProduct.Country:
            case TypeOfMetaProduct.Director:
            case TypeOfMetaProduct.FromURI:
            case TypeOfMetaProduct.Genre:
            case TypeOfMetaProduct.InRole:
            case TypeOfMetaProduct.Name:
            case TypeOfMetaProduct.PosterFromURI:
            case TypeOfMetaProduct.Type:
            case TypeOfMetaProduct.View:
            case TypeOfMetaProduct.NumberOfEpisode:
                sb.AppendLine(InputExtensions
                              .TextBox(html, String
                                       .Format("MetaData[{0}].String", num), meta.String, new { @class = "form-control" }).ToHtmlString());
                break;

            case TypeOfMetaProduct.Begin:
            case TypeOfMetaProduct.End:
                sb.AppendLine(InputExtensions
                              .TextBox(html, String
                                       .Format("MetaData[{0}].DateTime", num), meta.Date.ToShortDateString(), new { @class = "form-control" }).ToHtmlString());
                break;

            case TypeOfMetaProduct.Ended:
                sb.AppendLine(InputExtensions
                              .CheckBox(html, String
                                        .Format("MetaData[{0}].Bool", num), meta.Bool, new { @class = "form-control" }).ToHtmlString());
                break;
            }
            sb.AppendLine("<span class=\"input-group-btn\">");
            sb.AppendLine(String.Format(
                              "<input name =\"MetaData[{0}].String\" type=\"submit\" value=\"Удалить\" class=\"btn btn-success\">", num));
            sb.AppendLine("</span>");
            sb.AppendLine("</div>");
            sb.AppendLine("</div>");
            sb.AppendLine("</div>");
            return(new MvcHtmlString(sb.ToString()));
        }
コード例 #9
0
        public static MvcHtmlString ZN_PasswordFor <TModel, TProperty>(this HtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TProperty> > expression, object htmlAttributes = null)
        {
            TagBuilder tagResult = new TagBuilder("div");

            tagResult.MergeAttribute("class", "form-group");
            tagResult.InnerHtml += LabelExtensions.LabelFor(htmlHelper, expression).ToHtmlString();
            tagResult.InnerHtml += InputExtensions.PasswordFor(htmlHelper, expression, htmlAttributes).ToHtmlString();
            tagResult.InnerHtml += ValidationExtensions.ValidationMessageFor(htmlHelper, expression, "", new { @class = "error" }).ToHtmlString();
            return(tagResult.ToHtml());
        }
コード例 #10
0
        public static MvcHtmlString ZN_DropDownListFor <TModel, TProperty>(this HtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TProperty> > expression, IEnumerable <SelectListItem> selectList, string optionLabel = null, object htmlAttributes = null)
        {
            TagBuilder tagResult = new TagBuilder("div");

            tagResult.MergeAttribute("class", "form-group");
            tagResult.InnerHtml += LabelExtensions.LabelFor(htmlHelper, expression).ToHtmlString();
            tagResult.InnerHtml += System.Web.Mvc.Html.SelectExtensions.DropDownListFor(htmlHelper, expression, selectList, optionLabel, htmlAttributes).ToHtmlString();
            tagResult.InnerHtml += ValidationExtensions.ValidationMessageFor(htmlHelper, expression, "", new { @class = "error" }).ToHtmlString();
            return(tagResult.ToHtml());
        }
コード例 #11
0
ファイル: CustomHelper.cs プロジェクト: prjromy/CollectionApp
        public static MvcHtmlString ChqLabelFor <TModel, TValue>(this HtmlHelper <TModel> html, Expression <Func <TModel, TValue> > expression, object htmlAttributes)
        {
            var    metadata          = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
            string resolvedLabelText = metadata.DisplayName ?? metadata.PropertyName;

            if (metadata.IsRequired)
            {
                resolvedLabelText += "*";
            }
            return(LabelExtensions.LabelFor <TModel, TValue>(html, expression, resolvedLabelText, htmlAttributes));
        }
コード例 #12
0
        public static MvcHtmlString JQM_SelectMenu(this HtmlHelper htmlHelper, string name, string label, MultiSelectList values, string optionLabel = null, SelectMenuConfig config = null)
        {
            TagBuilder tagResult = new TagBuilder("div");

            tagResult.MergeAttribute("class", "ui-field-contain");
            if (config == null)
            {
                config = new SelectMenuConfig();
            }

            tagResult.InnerHtml += LabelExtensions.Label(htmlHelper, "", label).ToHtmlString();
            tagResult.InnerHtml += SelectExtensions.DropDownList(htmlHelper, name, values, optionLabel, (config != null) ? config.GetAttributes() : null).ToHtmlString();
            return(tagResult.ToHtml());
        }
コード例 #13
0
        public static MvcHtmlString JQM_SelectMenuGroupedFor <TModel, TProperty>(this HtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TProperty> > expression, IEnumerable <GroupedSelectListItem> selectList, string optionLabel = null, SelectMenuConfig config = null)
        {
            TagBuilder tagResult = new TagBuilder("div");

            tagResult.MergeAttribute("class", "ui-field-contain");
            if (config == null)
            {
                config = new SelectMenuConfig();
            }

            tagResult.InnerHtml += LabelExtensions.LabelFor(htmlHelper, expression).ToHtmlString();
            tagResult.InnerHtml += HtmlHelpers.DropDownGroupListFor(htmlHelper, expression, selectList, optionLabel, (config != null) ? config.GetAttributes() : null).ToHtmlString();
            tagResult.InnerHtml += ValidationExtensions.ValidationMessageFor(htmlHelper, expression).ToHtmlString();
            return(tagResult.ToHtml());
        }
コード例 #14
0
        public static IHtmlString CheckBoxForExtended <TModel>(this HtmlHelper <TModel> html, Expression <Func <TModel, bool> > property)
        {
            TagBuilder outerDiv = new TagBuilder("div");

            outerDiv.AddCssClass("custom-control custom-checkbox form-control-lg");

            MvcHtmlString checkbox = InputExtensions.CheckBoxFor(html, property, new { @class = "custom-control-input" });
            MvcHtmlString label    = LabelExtensions.LabelFor(html, property, htmlAttributes: new { @class = "custom-control-label" });

            StringBuilder htmlBuilder = new StringBuilder();

            htmlBuilder.Append(checkbox.ToHtmlString());
            htmlBuilder.Append(label.ToHtmlString());

            outerDiv.InnerHtml = htmlBuilder.ToString();
            return(MvcHtmlString.Create(outerDiv.ToString(TagRenderMode.Normal)));
        }
コード例 #15
0
        public static MvcHtmlString BSFGForEnumDDL <TModel>(
            this HtmlHelper <TModel> html,
            Type MyEnum,
            string ValName,
            string label,
            int col_sm,
            string btn_value,
            string btn_name,
            string defaultvalue)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("<div class=\"form-group\">");
            sb.AppendLine(LabelExtensions
                          .Label(html, ValName, label, new { @class = String.Format("col-sm-{0} control-label", 12 - col_sm) }).ToHtmlString());
            sb.AppendLine(String.Format("<div class=\"col-sm-{0}\">", col_sm));
            sb.AppendLine("<div class=\"input-group\">");
            sb.Append(String.Format("<select name=\"{0}\" class=\"form-control\">", ValName));
            foreach (var iten in Enum.GetValues(MyEnum))
            {
                var value = iten.ToString();
                var name  = (MyEnum.GetMember(iten.ToString()).First().GetCustomAttributes(typeof(DisplayAttribute), true).First() as DisplayAttribute).Name;
                //var da = iten.GetCustomAttributes(typeof(DisplayAttribute),true);
                if (value == defaultvalue)
                {
                    sb.Append(String.Format("<option selected value=\"{0}\">", value));
                }
                else
                {
                    sb.Append(String.Format("<option value=\"{0}\">", value));
                }
                sb.Append(name);
                sb.Append("</option>");
            }
            sb.Append("</select>");
            sb.AppendLine("<span class=\"input-group-btn\">");
            sb.AppendLine(String.Format(
                              "<input name =\"{0}\" type=\"submit\" value=\"{1}\" class=\"btn btn-success\">",
                              btn_name, btn_value));
            sb.AppendLine("</span>");
            sb.AppendLine("</div>");
            sb.AppendLine("</div>");
            sb.AppendLine("</div>");
            return(new MvcHtmlString(sb.ToString()));
        }
コード例 #16
0
        public static string Object(HtmlHelper html)
        {
            ValidationHelper validation = new ValidationHelper(html.Context);

            ModelMetadata metadata = html.Context.ViewData.Metadata;
            object        model    = html.Context.ViewData.Model;
            Type          type     = html.Context.ViewData.Template.Type;

            if (model == null)
            {
                return(metadata.NullDisplayText);
            }

            if (html.Context.ViewData.Template.Depth > 1)
            {
                return(metadata.GetDisplayText(model));
            }

            StringBuilder builder = new StringBuilder();

            foreach (ModelMetadata property in metadata.Properties
                     .Where(pm => pm.Visible).OrderBy(pm => pm.FieldOrder))
            {
                object propertyValue = (model == null) ? null : property.GetModel(model);
                string elementName   = html.Context.ViewData.Template.GetHtmlElementName(property.PropertyName);

                if (!property.HideSurroundingChrome)
                {
                    builder.Append(LabelExtensions.Render(html.Context, property.PropertyName, property));
                }

                builder.Append(html.Templates.Render(W.DataBoundControlMode.Edit,
                                                     property, null, property.PropertyName, propertyValue));

                validation.Messages(elementName, (errors) => {
                    builder.Append(new HtmlElementBuilder("ul",
                                                          new { @class = "validation-error" },
                                                          string.Join(Environment.NewLine, errors.Select(
                                                                          error => new HtmlElementBuilder("li", null, error.Message).ToString()
                                                                          ).ToArray())
                                                          ));
                });
            }
            return(builder.ToString());
        }
コード例 #17
0
        public static MvcHtmlString BSFGForNumericBox <TModel, TProperty>(
            this HtmlHelper <TModel> html,
            Expression <Func <TModel, TProperty> > expression,
            int col_sm)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("<div class=\"form-group\">");
            sb.AppendLine(LabelExtensions
                          .LabelFor(html, expression, new { @class = String.Format("col-sm-{0} control-label", 12 - col_sm) }).ToHtmlString());
            sb.AppendLine(String.Format("<div class=\"col-sm-{0}\">", col_sm));
            sb.AppendLine(InputExtensions
                          .TextBoxFor(html, expression, new { @class = "form-control" }).ToHtmlString());
            sb.AppendLine(ValidationExtensions
                          .ValidationMessageFor(html, expression).ToHtmlString());
            sb.AppendLine("</div>");
            sb.AppendLine("</div>");
            return(new MvcHtmlString(sb.ToString()));
        }
コード例 #18
0
        public static MvcHtmlString CultureSpecificLabel <TModel, TValue>(this HtmlHelper <TModel> html, Expression <Func <TModel, TValue> > expression, string module = "")
        {
            string moduleName = "";

            if (module != "")
            {
                moduleName = module;
            }

            else if (html.ViewBag.ModuleName != null)
            {
                moduleName = html.ViewBag.ModuleName;
            }
            else if (html.ViewData.Model != null)
            {
                var moduelNameAttribute = (html.ViewData.Model.GetType().GetCustomAttributes(false).FirstOrDefault());

                if (moduelNameAttribute is CultureModuleAttribute)
                {
                    moduleName = ((CultureModuleAttribute)moduelNameAttribute).ModuleName;
                }
            }

            if (!string.IsNullOrEmpty(moduleName))
            {
                var s = ((MemberExpression)expression.Body).Member;
                var e = s.GetCustomAttributes(false).Where(x => x.GetType() == typeof(System.ComponentModel.DataAnnotations.DisplayAttribute)).FirstOrDefault();
                if (e == null)
                {
                    return(LabelExtensions.LabelFor(html, expression, CommonFunctions.GetGlobalizedLabel(moduleName, s.Name)));
                }
                else
                {
                    // var x = (System.ComponentModel.DataAnnotations.DisplayAttribute)e;
                    return(LabelExtensions.LabelFor(html, expression, CommonFunctions.GetGlobalizedLabel(moduleName, s.Name)));
                }
            }
            else
            {
                return(LabelExtensions.LabelFor(html, expression));
            }
        }
コード例 #19
0
        public static MvcHtmlString BSFGForListTextBox <TModel, TProperty>(
            this HtmlHelper <TModel> html,
            Expression <Func <TModel, TProperty> > expression,
            int col_sm,
            string PlaceHolder)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("<div class=\"form-group\">");
            sb.AppendLine(LabelExtensions
                          .LabelFor(html, expression, new { @class = String.Format("col-sm-{0} control-label", 12 - col_sm) }).ToHtmlString());

            string name = (expression.Body as MemberExpression).Member.Name;

            sb.AppendLine(String.Format("<div class=\"col-sm-{0} js-for-{1}\">", col_sm, name));
            IEnumerable <object> list = expression.Compile()(html.ViewData.Model) as IEnumerable <object>;
            int i = 0; if (list != null)

            {
                foreach (var it in list)
                {
                    sb.AppendLine("<div class=\"input-group\">");
                    sb.AppendLine(InputExtensions
                                  .TextBox(html, String.Format("{0}[{1}]", name, i), it,
                                           new { @class = "form-control", placeholder = PlaceHolder }
                                           ).ToHtmlString());
                    sb.AppendLine("<span class=\"input-group-btn\">");
                    sb.AppendLine(String.Format(
                                      "<input type=\"button\" value=\"Удалить\" class=\"btn btn-danger\" onclick=\"BSFGRemove('{0}','{1}')\">", name, i));
                    sb.AppendLine("</span>");
                    sb.AppendLine("</div>");
                    i++;
                }
            }


            sb.AppendLine(String.Format(
                              "<input type=\"button\" value=\"Добавить\" class=\"form-control btn btn-xs btn-success\" onclick=\"BSFGAdd('{0}')\" >", name));
            sb.AppendLine("</div>");
            sb.AppendLine("</div>");
            return(new MvcHtmlString(sb.ToString()));
        }
コード例 #20
0
        public static MvcHtmlString BSFGLabel <TModel, TProperty>(
            this HtmlHelper <TModel> html,
            Expression <Func <TModel, TProperty> > expression,
            int col_sm)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("<div class=\"form-group\">");
            sb.AppendLine(LabelExtensions
                          .LabelFor(html, expression, new { @class = String.Format("col-sm-{0} control-label", 12 - col_sm) }).ToHtmlString());
            sb.AppendLine(String.Format("<div class=\"col-sm-{0}\">", col_sm));
            string value;

            try { value = expression.Compile()(html.ViewData.Model).ToString(); }
            catch { value = ""; }
            sb.AppendLine(LabelExtensions
                          .Label(html, value, new { @class = "form-control" }).ToHtmlString());
            sb.AppendLine("</div>");
            sb.AppendLine("</div>");
            return(new MvcHtmlString(sb.ToString()));
        }
コード例 #21
0
        public static MvcHtmlString FieldFor <TModel, TValue>(this HtmlHelper <TModel> helper, Expression <Func <TModel, TValue> > expression)
        {
            MvcHtmlString label  = LabelExtensions.LabelFor(helper, expression, new { @class = "control-label col-md-2" });
            MvcHtmlString editor = EditorExtensions.EditorFor(helper, expression, new { htmlAttributes = new { @class = "form-control" } })
                                   MvcHtmlString validation = ValidationExtensions.ValidationMessageFor(helper, expression, null, new { @class = "text-danger" });
            StringBuilder html = new StringBuilder();

            html.Append(editor);
            html.Append(validation);
            TagBuilder innerDiv = new TagBuilder("div");

            innerDiv.AddCssClass("col-md-10");
            innerDiv.InnerHtml = html.ToString();
            html = new StringBuilder();
            html.Append(label);
            html.Append(innerDiv.ToString());
            TagBuilder outerDiv = new TagBuilder("div");

            outerDiv.AddCssClass("form-group");
            outerDiv.InnerHtml = html.ToString();
            return(MvcHtmlString.Create(outerDiv.ToString()));
        }
コード例 #22
0
        public static MvcHtmlString BootstrapDropDownFor <TModel, TValue>(this HtmlHelper <TModel> helper, Expression <Func <TModel, TValue> > expression, IList <SelectListItem> selectList)
        {
            MvcHtmlString label      = LabelExtensions.LabelFor(helper, expression, new { @class = "control-label col-md-2" });
            MvcHtmlString select     = SelectExtensions.DropDownListFor(helper, expression, selectList, new { @id = "dropdown" });
            MvcHtmlString validation = ValidationExtensions.ValidationMessageFor(helper, expression, null, new { @class = "text-danger" });
            StringBuilder innerHtml  = new StringBuilder();

            innerHtml.Append(select);
            innerHtml.Append(validation);
            TagBuilder innerDiv = new TagBuilder("div");

            innerDiv.AddCssClass("col-md-10");
            innerDiv.InnerHtml = innerHtml.ToString();
            StringBuilder outerHtml = new StringBuilder();

            outerHtml.Append(label);
            outerHtml.Append(innerDiv.ToString());
            TagBuilder outerDiv = new TagBuilder("div");

            outerDiv.AddCssClass("form-group");
            outerDiv.InnerHtml = outerHtml.ToString();
            return(MvcHtmlString.Create(outerDiv.ToString()));
        }
コード例 #23
0
        public static IHtmlString TextBoxForExtended <TModel, TValue>(this HtmlHelper <TModel> html, Expression <Func <TModel, TValue> > property)
        {
            TagBuilder outerDiv = new TagBuilder("div");

            outerDiv.AddCssClass("input-group mb-3");

            TagBuilder innerDiv = new TagBuilder("div");

            innerDiv.AddCssClass("input-group-prepend");

            MvcHtmlString label = LabelExtensions.LabelFor(html, property, new { @class = "input-group-text" });

            MvcHtmlString textBox = InputExtensions.TextBoxFor(html, property, new { @class = "form-control" });

            StringBuilder htmlBuilder = new StringBuilder();

            htmlBuilder.Append(innerDiv.ToString(TagRenderMode.StartTag));
            htmlBuilder.Append(label.ToHtmlString());
            htmlBuilder.Append(innerDiv.ToString(TagRenderMode.EndTag));
            htmlBuilder.Append(textBox.ToHtmlString());

            outerDiv.InnerHtml = htmlBuilder.ToString();
            return(MvcHtmlString.Create(outerDiv.ToString(TagRenderMode.Normal)));
        }
コード例 #24
0
        public static MvcHtmlString LocalizedLabel(this HtmlHelper helper, string keyPath, object attributes)
        {
            string required = ResourceManager.GetValidators(keyPath).Contains("required") ? "*" : "";

            return(LabelExtensions.Label(helper, "", ResourceManager.GetString(keyPath) + required, attributes));
        }
コード例 #25
0
        public static MvcHtmlString JQM_AutoCompleteFor <TModel, TProperty>(this HtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TProperty> > expression, string placeHolder, string serviceUrl, string pageName)
        {
            StringBuilder    clientScript = new StringBuilder();
            MemberExpression body         = expression.Body as MemberExpression;
            string           fieldName    = body.Member.Name;
            string           filterName   = String.Format("FilterFor{0}", fieldName);
            ListViewConfig   config       = new ListViewConfig(filterName)
            {
                IsFilterable = true, PlaceHolder = placeHolder, Inset = true
            };
            TagBuilder tagResult = new TagBuilder("div");
            TagBuilder ulResult  = new TagBuilder("ul");

            clientScript.AppendFormat("    function SelectResult{0}(keyField, valueField, key, value) {{\n", filterName);
            clientScript.AppendLine("        $(\"#\" + keyField).val(key);");
            clientScript.AppendLine("        $(\"#\" + valueField).val(value);");
            clientScript.AppendFormat("        $(\"#{0}\").html(\"\");\n", filterName);
            clientScript.AppendLine("    }");

            clientScript.AppendFormat("$(document).on(\"pagecreate\", \"#{0}\", function () {{\n", pageName);
            clientScript.AppendFormat("    $(\"#{0}\").on(\"filterablebeforefilter\", function (e, data) {{\n", filterName);
            clientScript.AppendLine("        var $ul = $(this),");
            clientScript.AppendLine("               $input = $(data.input),");
            clientScript.AppendLine("               value = $input.val(),");
            clientScript.AppendLine("               html = \"\";");
            clientScript.AppendLine("       $ul.html(\"\");");
            clientScript.AppendFormat("     $input.attr('id', 'Description{0}');\n", filterName);
            clientScript.AppendFormat("     $input.attr('name', 'Description{0}');\n", filterName);
            clientScript.AppendLine("       if ($input.val().length >= 4 )");
            clientScript.AppendLine("       {");
            clientScript.AppendLine("        $.ajax({");
            clientScript.AppendFormat("          url: \"{0}\",\n", serviceUrl);
            clientScript.AppendLine("            dataType: \"json\",");
            clientScript.AppendLine("            data: {");
            //clientScript.AppendLine("                minLength: 4,");
            //clientScript.AppendLine("                maxRows: 12,");
            clientScript.AppendLine("                name_startsWith: $input.val()");
            clientScript.AppendLine("            },");
            clientScript.AppendLine("            success: function (data) {");
            clientScript.AppendLine("                $.each(data.ResponseData.Result[0], function (i, val) {");
            clientScript.AppendFormat("                    html +=  \"<li><a href=\\\"javascript:SelectResult{0}('{1}', 'Description{0}', \" + val.id + \", '\" + val.label + \"')\\\" >\" + val.label + \"</a></li>\"", filterName, fieldName);
            clientScript.AppendLine("                });");
            clientScript.AppendLine("                $ul.html(html);");
            clientScript.AppendLine("                $ul.listview(\"refresh\");");
            clientScript.AppendLine("                $ul.trigger(\"updatelayout\");");
            clientScript.AppendLine("            }");
            clientScript.AppendLine("        });");
            clientScript.AppendLine("      }");
            clientScript.AppendLine("    });");
            clientScript.AppendLine("});");

            htmlHelper.ViewDataContainer.ViewData.Add("Script", clientScript.ToString());
            tagResult.MergeAttribute("class", "ui-field-contain");
            ulResult.MergeAttributes(config.GetAttributes());
            ulResult.MergeAttribute("id", filterName);

            tagResult.InnerHtml += LabelExtensions.LabelFor(htmlHelper, expression).ToHtmlString();
            tagResult.InnerHtml += InputExtensions.HiddenFor(htmlHelper, expression).ToHtmlString();
            tagResult.InnerHtml += ulResult.ToHtml();
            tagResult.InnerHtml += ValidationExtensions.ValidationMessageFor(htmlHelper, expression).ToHtmlString();
            return(tagResult.ToHtml());
        }
コード例 #26
0
 public static MvcHtmlString LabelForModel(this HtmlHelper htmlHelper)
 {
     return(LabelExtensions.LabelForModel(htmlHelper));
 }
コード例 #27
0
        // LabelExtensions

        public static MvcHtmlString Label(this HtmlHelper htmlHelper, string expression)
        {
            return(LabelExtensions.Label(htmlHelper, expression));
        }
コード例 #28
0
        public static MvcHtmlString ChangeableFor <TModel, TValue, TType>(this HtmlHelper <TModel> html, Expression <Func <TModel, TValue> > expression, Changeable <TType> changeable)
        {
            // helper to take our Changeable<T> property, and create lambdas to get
            var controller = html.ViewContext.Controller;
            var actionName = controller.ValueProvider.GetValue("action").RawValue.ToString();
            var allMethods = controller.GetType().GetMethods();
            var methods    = allMethods.Where(m => m.Name == actionName);

            foreach (var method in methods)
            {
                var attributes = method.GetCustomAttributes(true);
                foreach (var attribute in attributes)
                {
                    if (attribute.GetType().Equals(typeof(HttpPostAttribute)))
                    {
                        var a = attribute;
                    }
                }
            }
            var name = ExpressionHelper.GetExpressionText(expression);

            if (String.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name", "Name cannot be null");
            }

            // get the metadata for our Changeable<T> property
            var metadata      = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
            var type          = metadata.ModelType;
            var containerType = metadata.ContainerType;

            // create a lambda expression to get the value of our Changeable<T>
            var        arg  = Expression.Parameter(containerType, "x");
            Expression expr = arg;

            expr = Expression.Property(expr, name);
            expr = Expression.Property(expr, "Value");
            var funcExpr           = Expression.Lambda(expr, arg) as Expression <Func <TModel, TType> >;
            var valueModelMetadata = ModelMetadata.FromLambdaExpression(funcExpr, html.ViewData);

            // create a lambda expression to get whether our Changeable<T> has changed
            Expression exprChanged = arg;

            exprChanged = Expression.Property(exprChanged, name);
            exprChanged = Expression.Property(exprChanged, "Changed");
            var funcExprChanged = Expression.Lambda(exprChanged, arg) as Expression <Func <TModel, bool> >;

            // use a stringbuilder for our HTML markup
            // return label, checkbox, hidden input, EDITOR, and validation message field
            // NOTE:  this is over-simplified markup!
            // ALSO NOTE:  the EditorFor is passed the strongly typed model taken from our T.  Bonus!
            var htmlSb = new StringBuilder("\n");

            htmlSb.Append(LabelExtensions.Label(html, metadata.GetDisplayName()));
            htmlSb.Append("<br />\n");
            htmlSb.Append(InputExtensions.CheckBoxFor(html, funcExprChanged));
            htmlSb.Append(" Changed<br />\n");
            htmlSb.Append(InputExtensions.Hidden(html, name + ".OldValue", valueModelMetadata.Model) + "\n");
            htmlSb.Append(EditorExtensions.EditorFor(html, funcExpr, new KeyValuePair <string, object>("parentMetadata", metadata))); //new object[] { "parentMetadata", metadata }));
            htmlSb.Append(ValidationExtensions.ValidationMessageFor(html, funcExpr));
            htmlSb.Append("<br />\n");

            return(new MvcHtmlString(htmlSb.ToString()));
        }