Пример #1
0
        public static MvcHtmlString NgTextBoxFor <TModel, TProperty>(this HtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TProperty> > expression, string cssClass, IDictionary <string, object> htmlAttributes)
        {
            var ngModelExplicit = htmlAttributes != null && htmlAttributes.ContainsKey("ng-model");

            htmlAttributes = HtmlHelperExtension.AddClientValidation(htmlHelper, expression, htmlAttributes);
            if (!String.IsNullOrEmpty(cssClass))
            {
                htmlAttributes["class"] = cssClass;
            }
            if (htmlAttributes.ContainsKey("type") && (String)htmlAttributes["type"] == "date")
            {
                htmlAttributes.Remove("type");
                htmlAttributes["uib-datepicker-popup"] = "yyyy-MM-dd";
                if (!htmlAttributes.ContainsKey("pb-datepicker-button"))
                {
                    htmlAttributes["pb-datepicker-button"] = "yes";
                }
            }
            else
            if (!htmlAttributes.ContainsKey("ng-model-options"))
            {
                htmlAttributes["ng-model-options"] = "{ updateOn: 'blur' }";
            }
            return(HtmlHelperExtension.AdjustName(System.Web.Mvc.Html.InputExtensions.TextBoxFor(htmlHelper, expression, htmlAttributes), htmlAttributes, ngModelExplicit));
        }
Пример #2
0
        public static MvcHtmlString NgHiddenFor <TModel, TProperty>(this HtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TProperty> > expression, IDictionary <string, object> htmlAttributes)
        {
            if (expression == null)
            {
                throw new ArgumentNullException("expression");
            }
            var ngModelExplicit = htmlAttributes != null && htmlAttributes.ContainsKey("ng-model");

            htmlAttributes = HtmlHelperExtension.AddClientValidation(htmlHelper, expression, htmlAttributes);
            var value = "{{" + htmlAttributes["ng-model"] + "}}";

            htmlAttributes.Remove("ng-model");
            htmlAttributes.Remove("type");
            return(HtmlHelperExtension.AdjustName(System.Web.Mvc.Html.InputExtensions.Hidden(htmlHelper, ExpressionHelper.GetExpressionText(expression), value, htmlAttributes), htmlAttributes, ngModelExplicit));
        }
Пример #3
0
        public static MvcHtmlString NgTextAreaFor <TModel, TProperty>(this HtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TProperty> > expression, string cssClass, int rows, int columns, IDictionary <string, object> htmlAttributes)
        {
            if (expression == null)
            {
                throw new ArgumentNullException("expression");
            }
            var ngModelExplicit = htmlAttributes != null && htmlAttributes.ContainsKey("ng-model");

            htmlAttributes = HtmlHelperExtension.AddClientValidation(htmlHelper, expression, htmlAttributes);
            if (!String.IsNullOrEmpty(cssClass))
            {
                htmlAttributes["class"] = cssClass;
            }
            if (!htmlAttributes.ContainsKey("ng-model-options"))
            {
                htmlAttributes["ng-model-options"] = "{ updateOn: 'blur' }";
            }
            return(HtmlHelperExtension.AdjustName(System.Web.Mvc.Html.TextAreaExtensions.TextAreaFor(htmlHelper, expression, rows, columns, htmlAttributes), htmlAttributes, ngModelExplicit));
        }
Пример #4
0
        private static MvcHtmlString NgSelectHelper <TModel, TProperty>(this HtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TProperty> > expression, String ngSelectList, string optionLabel, string cssClass, IDictionary <string, object> htmlAttributes)
        {
            if (expression == null)
            {
                throw new ArgumentNullException("expression");
            }
            var    ngModelExplicit = htmlAttributes != null && htmlAttributes.ContainsKey("ng-model");
            var    name            = ExpressionHelper.GetExpressionText(expression);
            string fullName        = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name);

            if (String.IsNullOrEmpty(fullName))
            {
                throw new ArgumentException("MvcResources.Common_NullOrEmpty", "name");
            }
            TagBuilder opnBuilder = null;
            var        opns       = "";
            var        ptype      = typeof(TProperty);

            if (ptype.IsEnum || Nullable.GetUnderlyingType(ptype) != null)
            {
                if (!ptype.IsEnum)
                {
                    ptype = Nullable.GetUnderlyingType(ptype);
                }
                htmlAttributes = HtmlHelperExtension.AddClientValidation(htmlHelper, expression, htmlAttributes);
                if (ngSelectList != null || optionLabel != null)
                {
                    opnBuilder = new TagBuilder("option")
                    {
                        InnerHtml = HttpUtility.HtmlEncode(ngSelectList ?? optionLabel)
                    };
                    opnBuilder.MergeAttribute("value", "");
                    opnBuilder.MergeAttribute("translate", "");
                    opns = opnBuilder.ToString(TagRenderMode.Normal);
                }
                ngSelectList = HtmlHelperExtension.DictJsName + "." + ptype.FullName.Substring(ptype.FullName.LastIndexOf(".") + 1).Replace("+", "_");
            }
            else if (ngSelectList == null && (htmlAttributes == null || !htmlAttributes.ContainsKey("ng-options")))
            {
                throw new ArgumentNullException("ng-options or ngSelectList");
            }
            else
            {
                htmlAttributes = HtmlHelperExtension.AddNgModel(htmlHelper, name + ".Id", htmlAttributes);
                htmlAttributes = HtmlHelperExtension.AddClientValidation(htmlHelper, expression, htmlAttributes);
                if (!ngModelExplicit)
                {
                    fullName = fullName + ".Id";
                }
                if (optionLabel != null)
                {
                    opnBuilder = new TagBuilder("option")
                    {
                        InnerHtml = HttpUtility.HtmlEncode(optionLabel)
                    };
                    opnBuilder.MergeAttribute("value", "");
                    opnBuilder.MergeAttribute("translate", "");
                    opns = opnBuilder.ToString(TagRenderMode.Normal);
                }
            }
            if (!String.IsNullOrEmpty(cssClass))
            {
                htmlAttributes["class"] = cssClass;
            }
            TagBuilder tagBuilder = new TagBuilder("select");

            if (opns != "")
            {
                tagBuilder.InnerHtml = opns;
            }
            ;
            tagBuilder.MergeAttributes(htmlAttributes);
            tagBuilder.MergeAttribute("name", fullName, true /* replaceExisting */);
            tagBuilder.GenerateId(fullName);
            if (ptype.IsEnum)
            {
                var ngoptions = "value as label for (label, value) in " + ngSelectList;
                if (htmlAttributes.ContainsKey("pb-AddOptions"))
                {
                    ngoptions = ngoptions + "|SelectAddOptions:" + ((String)htmlAttributes["pb-AddOptions"]);
                }
                tagBuilder.MergeAttribute("ng-options", ngoptions);
            }
            else if (!htmlAttributes.ContainsKey("ng-options"))
            {
                if (ngSelectList.Contains(" for "))
                {
                    tagBuilder.MergeAttribute("ng-options", ngSelectList);
                }
                else
                {
                    var ngoptions = "item.Id as item.RefText for item in " + ngSelectList;
                    if (htmlAttributes.ContainsKey("pb-EnforceMatch"))
                    {
                        ngoptions = ngoptions + "|RefSelectEnforceMatch:" + ((String)htmlAttributes["ng-model"]).Replace(".Id", "");                                               // +" track by item.Id";
                    }
                    if (htmlAttributes.ContainsKey("pb-AddOptions"))
                    {
                        ngoptions = ngoptions + "|SelectAddOptions:" + ((String)htmlAttributes["pb-AddOptions"]);
                    }
                    tagBuilder.MergeAttribute("ng-options", ngoptions);
                }
            }

            return(HtmlHelperExtension.AdjustName(new MvcHtmlString(tagBuilder.ToString(TagRenderMode.Normal)), htmlAttributes, ngModelExplicit));
        }