示例#1
0
        public static MvcHtmlString NgTextBoxFor <TModel, TProperty>(this HtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TProperty> > expression, object htmlAttributes)
        {
            MemberInfo        member          = ((MemberExpression)expression.Body).Member;
            var               tagBuilder      = new TagBuilder("input");
            List <TagBuilder> validationSpans = new List <TagBuilder>();

            foreach (CustomAttributeData cs in member.CustomAttributes)
            {
                if (cs.AttributeType == typeof(DataTypeAttribute))
                {
                    string inputType = HelperConstants.DataTypeMapping[(DataType)cs.ConstructorArguments[0].Value];
                    tagBuilder.MergeAttribute("type", inputType);
                    tagBuilder.MergeAttribute("name", member.Name.ToLower());

                    CustomAttributeNamedArgument nameArg = cs.NamedArguments.Where(m => m.MemberName == "ErrorMessage").First();

                    if (nameArg != null)
                    {
                        validationSpans.Add(NgHtmlHelper.GetSpan(nameArg.TypedValue.Value.ToString(), member.Name.ToLower(), inputType));
                    }
                }
                else if (cs.AttributeType == typeof(RequiredAttribute))
                {
                    tagBuilder.MergeAttribute("required", string.Empty);

                    validationSpans.Add(NgHtmlHelper.GetRequiredSpan(cs, member));
                }
                else if (cs.AttributeType == typeof(MaxLengthAttribute))
                {
                    tagBuilder.MergeAttribute("maxlength", cs.ConstructorArguments[0].Value.ToString());
                }
            }

            if (member.CustomAttributes.Where(attr => attr.AttributeType == typeof(DataTypeAttribute)).Count() == 0)
            {
                tagBuilder.MergeAttribute("type", "text");
                tagBuilder.MergeAttribute("name", member.Name.ToLower());
            }

            NgHtmlHelper.SetHtmlAttributes(tagBuilder, htmlAttributes);

            tagBuilder.MergeAttribute("ng-model", member.ReflectedType.Name.ToCamelCase() + "." + member.Name);

            string finalHtml = tagBuilder.ToString(TagRenderMode.SelfClosing);

            foreach (TagBuilder span in validationSpans)
            {
                finalHtml += span.ToString(TagRenderMode.Normal);
            }
            return(MvcHtmlString.Create(finalHtml));
        }
示例#2
0
        public static MvcHtmlString NgListBoxFor <TModel, TProperty>(this HtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TProperty> > expression,
                                                                     string options, string valueField, string textField, object htmlAttributes)
        {
            MemberInfo member = ((MemberExpression)expression.Body).Member;

            var tagBuilder = new TagBuilder("select");
            List <TagBuilder> validationSpans = new List <TagBuilder>();

            tagBuilder.MergeAttribute("multiple", "multiple");

            tagBuilder.MergeAttribute("name", member.Name.ToLower());

            foreach (CustomAttributeData cs in member.CustomAttributes)
            {
                if (cs.AttributeType == typeof(RequiredAttribute))
                {
                    tagBuilder.MergeAttribute("required", string.Empty);

                    validationSpans.Add(NgHtmlHelper.GetRequiredSpan(cs, member));
                }
            }

            NgHtmlHelper.SetHtmlAttributes(tagBuilder, htmlAttributes);

            tagBuilder.MergeAttribute("ng-model", member.ReflectedType.Name.ToCamelCase() + "." + member.Name);

            TagBuilder optionBuilder = new TagBuilder("option");

            optionBuilder.MergeAttribute("ng-repeat", "opt in " + options);
            optionBuilder.MergeAttribute("value", "{{opt." + valueField + "}}");
            optionBuilder.MergeAttribute("text", "{{opt." + textField + "}}");
            optionBuilder.InnerHtml = "{{opt." + textField + "}}";

            tagBuilder.InnerHtml = optionBuilder.ToString();

            string finalHtml = tagBuilder.ToString(TagRenderMode.Normal);

            foreach (TagBuilder span in validationSpans)
            {
                finalHtml += span.ToString(TagRenderMode.Normal);
            }

            return(MvcHtmlString.Create(finalHtml));
        }
示例#3
0
        public static MvcHtmlString NgTextAreaFor <TModel, TProperty>(this HtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TProperty> > expression, object htmlAttributes)
        {
            MemberInfo member = ((MemberExpression)expression.Body).Member;

            var tagBuilder = new TagBuilder("textarea");

            tagBuilder.MergeAttribute("name", member.Name.ToLower());

            NgHtmlHelper.SetHtmlAttributes(tagBuilder, htmlAttributes);

            List <TagBuilder> validationSpans = new List <TagBuilder>();

            foreach (CustomAttributeData cs in member.CustomAttributes)
            {
                if (cs.AttributeType == typeof(RequiredAttribute))
                {
                    tagBuilder.MergeAttribute("Required", string.Empty);

                    validationSpans.Add(NgHtmlHelper.GetRequiredSpan(cs, member));
                }
                else if (cs.AttributeType == typeof(MaxLengthAttribute))
                {
                    tagBuilder.MergeAttribute("maxlength", cs.ConstructorArguments[0].Value.ToString());
                }
            }

            tagBuilder.MergeAttribute("ng-model", member.ReflectedType.Name.ToCamelCase() + "." + member.Name);

            string finalHtml = tagBuilder.ToString(TagRenderMode.Normal);

            foreach (TagBuilder span in validationSpans)
            {
                finalHtml += span.ToString(TagRenderMode.Normal);
            }

            return(MvcHtmlString.Create(finalHtml));
        }
示例#4
0
        public static MvcHtmlString NgDateBoxFor <TModel, TProperty>(this HtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TProperty> > expression,
                                                                     object htmlAttributes, string icon, string onClick, string format, string strIsOpen, string dateOptions)
        {
            MemberInfo member = ((MemberExpression)expression.Body).Member;

            var tagBuilder = new TagBuilder("input");

            List <TagBuilder> validationSpans = new List <TagBuilder>();

            foreach (CustomAttributeData cs in member.CustomAttributes)
            {
                if (cs.AttributeType == typeof(DataTypeAttribute))
                {
                    string inputType = HelperConstants.DataTypeMapping[(DataType)cs.ConstructorArguments[0].Value];
                    tagBuilder.MergeAttribute("type", inputType);
                    tagBuilder.MergeAttribute("name", member.Name.ToLower());

                    CustomAttributeNamedArgument nameArg = cs.NamedArguments.Where(m => m.MemberName == "ErrorMessage").First();

                    if (nameArg != null)
                    {
                        validationSpans.Add(NgHtmlHelper.GetSpan(nameArg.TypedValue.Value.ToString(), member.Name.ToLower(), "date"));
                    }
                }
                else if (cs.AttributeType == typeof(RequiredAttribute))
                {
                    tagBuilder.MergeAttribute("ng-required", "true");

                    validationSpans.Add(NgHtmlHelper.GetRequiredSpan(cs, member));
                }
            }

            if (member.CustomAttributes.Where(attr => attr.AttributeType == typeof(DataTypeAttribute)).Count() == 0)
            {
                tagBuilder.MergeAttribute("type", "text");
                tagBuilder.MergeAttribute("name", member.Name.ToLower());
            }
            tagBuilder.MergeAttribute("readonly", "readonly");
            RouteValueDictionary htmlAttr = new RouteValueDictionary(htmlAttributes);

            NgHtmlHelper.SetHtmlAttributes(tagBuilder, htmlAttributes);

            tagBuilder.MergeAttribute("ng-model", member.ReflectedType.Name.ToCamelCase() + "." + member.Name);

            if (string.IsNullOrEmpty(format))
            {
                format = "MM/dd/yyyy";
            }
            tagBuilder.MergeAttribute("uib-datepicker-popup", format);

            tagBuilder.MergeAttribute("is-open", strIsOpen);

            tagBuilder.MergeAttribute("close-text", "Close");

            if (!string.IsNullOrEmpty(dateOptions))
            {
                tagBuilder.MergeAttribute("datepicker-options", dateOptions);
            }

            string finalHtml = string.Empty;

            if (!string.IsNullOrEmpty(icon))
            {
                var tagDiv = new TagBuilder("div");
                tagDiv.AddCssClass("input-group");
                tagDiv.MergeAttribute("id", member.Name.ToLower());

                var tagIconDiv = new TagBuilder("span");

                tagIconDiv.AddCssClass("input-group-btn");

                StringBuilder strBtnSpan = new StringBuilder();
                strBtnSpan.AppendFormat("<button type =\"button\" class=\"btn btn-default\" ng-click=\"{0}()\" ><i class=\"{1}\"></i></button>", onClick, icon);

                tagIconDiv.InnerHtml = strBtnSpan.ToString();

                tagDiv.InnerHtml = tagBuilder.ToString(TagRenderMode.SelfClosing) +
                                   tagIconDiv.ToString(TagRenderMode.Normal);

                finalHtml = tagDiv.ToString(TagRenderMode.Normal);
            }
            else
            {
                tagBuilder.MergeAttribute("id", member.Name.ToLower());
                finalHtml = tagBuilder.ToString(TagRenderMode.SelfClosing);
            }

            // tagBuilder

            foreach (TagBuilder span in validationSpans)
            {
                finalHtml += span.ToString(TagRenderMode.Normal);
            }
            return(MvcHtmlString.Create(finalHtml));
        }