Exemplo n.º 1
0
        public static MvcHtmlString CascadingDropDownListFor <TModel, TProperty>(
            this HtmlHelper <TModel> htmlHelper,
            Expression <Func <TModel, TProperty> > expression,
            string triggeredByPropertyWithId,
            string url,
            string ajaxActionParamName,
            string optionLabel = null,
            bool disabledWhenParentNotSelected = false,
            object htmlAttributes          = null,
            CascadeDropDownOptions options = null)
        {
            var dropDownElementName = htmlHelper.GetElementNameFromExpression(expression);
            var dropDownElementId   = GetDropDownElementId(htmlAttributes) ?? htmlHelper.GetElementIdFromExpression(expression);

            if (string.IsNullOrEmpty(dropDownElementName) || string.IsNullOrEmpty(dropDownElementId))
            {
                throw new ArgumentException("expression argument is invalid");
            }

            return(CascadingDropDownList(
                       htmlHelper,
                       dropDownElementName,
                       dropDownElementId,
                       triggeredByPropertyWithId,
                       url,
                       ajaxActionParamName,
                       GetPropStringValue(htmlHelper.ViewData.Model, expression),
                       optionLabel,
                       disabledWhenParentNotSelected,
                       htmlAttributes != null
                ? HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)
                : new RouteValueDictionary(),
                       options));
        }
Exemplo n.º 2
0
 public static MvcHtmlString CascadingDropDownList(
     this HtmlHelper htmlHelper,
     string inputName,
     string inputId,
     string triggeredByProperty,
     string url,
     string ajaxActionParamName,
     string optionLabel = null,
     bool disabledWhenParentNotSelected  = false,
     RouteValueDictionary htmlAttributes = null,
     CascadeDropDownOptions options      = null)
 {
     return(CascadingDropDownList(
                htmlHelper,
                inputName,
                inputId,
                triggeredByProperty,
                url,
                ajaxActionParamName,
                GetPropStringValue(htmlHelper.ViewData.Model, inputName),
                optionLabel,
                disabledWhenParentNotSelected,
                htmlAttributes,
                options));
 }
Exemplo n.º 3
0
        public static MvcHtmlString CascadingDropDownList <TModel, TProperty>(
            this HtmlHelper <TModel> htmlHelper,
            string inputName,
            string inputId,
            Expression <Func <TModel, TProperty> > triggeredByProperty,
            string url,
            string ajaxActionParamName,
            string optionLabel = null,
            bool disabledWhenParentNotSelected = false,
            object htmlAttributes          = null,
            CascadeDropDownOptions options = null)
        {
            var triggeredByPropId = htmlHelper.GetElementIdFromExpression(triggeredByProperty);

            if (string.IsNullOrEmpty(triggeredByPropId))
            {
                throw new ArgumentException("triggeredByProperty argument is invalid");
            }

            return(CascadingDropDownList(
                       htmlHelper,
                       inputName,
                       inputId,
                       triggeredByPropId,
                       url,
                       ajaxActionParamName,
                       optionLabel,
                       disabledWhenParentNotSelected,
                       htmlAttributes,
                       options));
        }
Exemplo n.º 4
0
 public static MvcHtmlString CascadingDropDownList(
     this HtmlHelper htmlHelper,
     string inputName,
     string inputId,
     string triggeredByProperty,
     string url,
     string ajaxActionParamName,
     string selectedValue,
     string optionLabel = null,
     bool disabledWhenParentNotSelected = false,
     object htmlAttributes          = null,
     CascadeDropDownOptions options = null)
 {
     return(CascadingDropDownList(
                htmlHelper,
                inputName,
                inputId,
                triggeredByProperty,
                url,
                ajaxActionParamName,
                selectedValue,
                optionLabel,
                disabledWhenParentNotSelected,
                htmlAttributes != null
         ? HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)
         : new RouteValueDictionary(),
                options));
 }
        public static MvcHtmlString CascadingDropDownList <TModel, TProperty>(
            this HtmlHelper htmlHelper,
            string inputName,
            string inputId,
            Expression <Func <TModel, TProperty> > triggeredByProperty,
            string url,
            string ajaxActionParamName,
            string optionLabel = null,
            bool disabledWhenParentNotSelected = false,
            object htmlAttributes          = null,
            CascadeDropDownOptions options = null)
        {
            MemberInfo triggerMemberInfo = GetMemberInfo(triggeredByProperty);

            if (triggerMemberInfo == null)
            {
                throw new ArgumentException("triggeredByProperty argument is invalid");
            }

            return(CascadingDropDownList(
                       htmlHelper,
                       inputName,
                       inputId,
                       triggerMemberInfo.Name,
                       url,
                       ajaxActionParamName,
                       optionLabel,
                       disabledWhenParentNotSelected,
                       htmlAttributes,
                       options));
        }
        private static MvcHtmlString CascadingDropDownList(
            this HtmlHelper htmlHelper,
            string inputName,
            string cascadeDdElementId,
            string triggeredByProperty,
            string url,
            string ajaxActionParamName,
            string selectedValue,
            string optionLabel = null,
            bool disabledWhenParentNotSelected  = false,
            RouteValueDictionary htmlAttributes = null,
            CascadeDropDownOptions options      = null)
        {
            if (htmlAttributes == null)
            {
                htmlAttributes = new RouteValueDictionary();
            }

            htmlAttributes.Add("data-cascade-dd-url", url);
            var setDisableString     = string.Empty;
            var removeDisabledString = string.Empty;

            if (optionLabel != null)
            {
                htmlAttributes.Add("data-option-lbl", optionLabel);
            }

            if (disabledWhenParentNotSelected)
            {
                htmlAttributes.Add("disabled", "disabled");
                setDisableString     = "targetElement.setAttribute('disabled','disabled');";
                removeDisabledString = "targetElement.removeAttribute('disabled');";
            }


            var defaultDropDownHtml = htmlHelper.DropDownList(
                inputName,
                new List <SelectListItem>(),
                optionLabel,
                htmlAttributes);

            var scriptBuilder = new StringBuilder();
            var optionLblStr  = optionLabel == null ? "''" : string.Format(@"'<option value="""">{0}</option>'", optionLabel);

            scriptBuilder.AppendFormat(Js1CreateInitFunction, cascadeDdElementId, triggeredByProperty, selectedValue, removeDisabledString, optionLblStr, setDisableString);
            ApplyJsonToSendString(ref scriptBuilder, ajaxActionParamName, options);
            ApplyRequestString(ref scriptBuilder, options);
            ApplyOnLoadString(ref scriptBuilder, options);
            ApplyErrorCallbackString(ref scriptBuilder, options);
            ApplySendRequestString(ref scriptBuilder, options);
            scriptBuilder.AppendFormat(Js7EndFormat, cascadeDdElementId);
            var script = string.Concat("<script>", scriptBuilder.ToString(), "</script>");

            return(new MvcHtmlString(string.Concat(defaultDropDownHtml.ToString(), Environment.NewLine, script)));
        }
Exemplo n.º 7
0
        private static void ApplyErrorCallbackString(ref StringBuilder builder, CascadeDropDownOptions options)
        {
            var onComplete = string.Empty;
            var onFailure  = string.Empty;

            if (options != null && (!string.IsNullOrEmpty(options.OnCompleteGetData) || !string.IsNullOrEmpty(options.OnFailureGetData)))
            {
                if (!string.IsNullOrEmpty(options.OnCompleteGetData))
                {
                    onComplete = string.Format("{0}(null, request.responseText);", options.OnCompleteGetData);
                }
                if (!string.IsNullOrEmpty(options.OnSuccessGetData))
                {
                    onFailure = string.Format("{0}(request.responseText, request.status, request.statusText);", options.OnFailureGetData);
                }
                builder.AppendFormat(Js5ErrorCallback, onComplete, onFailure);
            }
        }
        public static MvcHtmlString CascadingDropDownListFor <TModel, TProperty, TProperty2>(
            this HtmlHelper <TModel> htmlHelper,
            Expression <Func <TModel, TProperty> > expression,
            Expression <Func <TModel, TProperty2> > triggeredByProperty,
            string url,
            string ajaxActionParamName,
            string optionLabel = null,
            bool disabledWhenParentNotSelected = false,
            object htmlAttributes          = null,
            CascadeDropDownOptions options = null)
        {
            var triggerMemberInfo = GetMemberInfo(triggeredByProperty);
            var dropDownElement   = GetMemberInfo(expression);

            if (dropDownElement == null)
            {
                throw new ArgumentException("expression argument is invalid");
            }

            if (dropDownElement == null)
            {
                throw new ArgumentException("triggeredByProperty argument is invalid");
            }

            var dropDownElementName = dropDownElement.Name;
            var dropDownElementId   = GetDropDownElementId(htmlAttributes) ?? dropDownElement.Name;

            return(CascadingDropDownList(htmlHelper,
                                         dropDownElementName,
                                         dropDownElementId,
                                         triggerMemberInfo.Name,
                                         url,
                                         ajaxActionParamName,
                                         GetPropStringValue(htmlHelper.ViewData.Model, expression),
                                         optionLabel,
                                         disabledWhenParentNotSelected,
                                         htmlAttributes != null
                ? HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)
                : new RouteValueDictionary(),
                                         options));
        }
Exemplo n.º 9
0
        private static void ApplyOnLoadString(ref StringBuilder builder, CascadeDropDownOptions options)
        {
            var onComplete = string.Empty;
            var onSuccess  = string.Empty;
            var onFailure  = string.Empty;

            if (options != null)
            {
                if (!string.IsNullOrEmpty(options.OnCompleteGetData))
                {
                    onComplete = string.Format("{0}(data, null);", options.OnCompleteGetData);
                }
                if (!string.IsNullOrEmpty(options.OnSuccessGetData))
                {
                    onSuccess = string.Format("{0}(data);", options.OnSuccessGetData);
                }
                if (!string.IsNullOrEmpty(options.OnFailureGetData))
                {
                    onFailure = string.Format("{0}(request.responseText, request.status, request.statusText);", options.OnFailureGetData);
                }
            }
            builder.AppendFormat(Js4OnLoadFormat, onComplete, onSuccess, onFailure);
        }
Exemplo n.º 10
0
 private static void ApplySendRequestString(ref StringBuilder builder, CascadeDropDownOptions options)
 {
     builder.Append(options == null || options.HttpMethod == null || !options.HttpMethod.Equals("POST", StringComparison.OrdinalIgnoreCase) ?
                    Js6SendGetRequest : Js6SendPostRequest);
 }
Exemplo n.º 11
0
 private static void ApplyJsonToSendString(ref StringBuilder builder, string ajaxParam, CascadeDropDownOptions options)
 {
     builder.Append(options == null || string.IsNullOrEmpty(options.BeforeSend) ?
                    string.Format(Js2SimpleGenerateJsonToSendFormat, ajaxParam) :
                    string.Format(Js2GenerateJsonToSendFromFunctionFormat, options.BeforeSend, ajaxParam));
 }