示例#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));
        }
        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
        public static MvcHtmlString NgCheckBoxFor <TModel>(this HtmlHelper <TModel> htmlHelper, Expression <Func <TModel, bool> > expression, string cssClass, string value, IDictionary <string, object> htmlAttributes)
        {
            if (expression == null)
            {
                throw new ArgumentNullException("expression");
            }
            var ngModelExplicit = htmlAttributes != null && htmlAttributes.ContainsKey("ng-model");

            htmlAttributes = HtmlHelperExtension.AddNgModel(htmlHelper, expression, htmlAttributes);
            if (!String.IsNullOrEmpty(cssClass))
            {
                htmlAttributes["class"] = cssClass;
            }
            if (value != null)
            {
                htmlAttributes["ng-true-value"] = "'" + value + "'";
            }
            return(HtmlHelperExtension.AdjustName(System.Web.Mvc.Html.InputExtensions.CheckBoxFor(htmlHelper, expression, htmlAttributes), htmlAttributes, ngModelExplicit));
        }
        public static MvcHtmlString NgRadioButtonFor <TModel, TProperty>(this HtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TProperty> > expression, object value, string cssClass, IDictionary <string, object> htmlAttributes)
        {
            var ngModelExplicit = htmlAttributes != null && htmlAttributes.ContainsKey("ng-model");

            htmlAttributes = HtmlHelperExtension.AddNgModel(htmlHelper, expression, htmlAttributes);
            if (!String.IsNullOrEmpty(cssClass))
            {
                htmlAttributes["class"] = cssClass;
            }
            if (value == null)
            {
                value = "";
                htmlAttributes["ng-value"] = "null";
            }
            else if (value is bool)
            {
                htmlAttributes["ng-value"] = value.ToString().ToLower();
            }
            else
            {
                htmlAttributes["ng-value"] = "'" + value + "'";
            }
            return(HtmlHelperExtension.AdjustName(System.Web.Mvc.Html.InputExtensions.RadioButtonFor(htmlHelper, expression, value, htmlAttributes), htmlAttributes, ngModelExplicit));
        }
示例#6
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));
        }