Exemplo n.º 1
0
        /// <summary>
        /// Create a dropdownlist whose display will depend on a parent dropdownlist
        /// </summary>
        /// <param name="htmlHelper">The HTML helper</param>
        /// <param name="name">The name of the form field to return</param>
        /// <param name="selectList">A collection of System.Web.Mvc.SelectListItem objects that are used to populate the drop-down list</param>
        /// <param name="optionLabel">The text for a default empty item. This parameter can be null</param>
        /// <param name="htmlAttributes">An object that contains the HTML attributes to set for the element</param>
        /// <param name="listType">automaticaly add the javascript for a type of list</param>
        /// <returns>An HTML select element with an option subelement for each item in the list</returns>
        public static MvcHtmlString CascadingDropDownList(this HtmlHelper htmlHelper, string name, IEnumerable <CascadingSelectListItem> selectList, string optionLabel, object htmlAttributes = null, CascadingDropDownListType listType = CascadingDropDownListType.ChildList)
        {
            MvcHtmlString mvcHtmlString  = htmlHelper.DropDownList(name, selectList, optionLabel, htmlAttributes);
            string        dropDownListId = name;

            return(CascadingDropDownList(htmlHelper, selectList, optionLabel, mvcHtmlString, dropDownListId, listType));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Create a dropdownlist whose display will depend on a parent dropdownlist
        /// </summary>
        /// <param name="htmlHelper">The HTML helper</param>
        /// <param name="selectList">A collection of System.Web.Mvc.SelectListItem objects that are used to populate the drop-down list</param>
        /// <param name="optionLabel">The text for a default empty item. This parameter can be null</param>
        /// <param name="mvcHtmlString">An HTML select element with an option subelement for each item</param>
        /// <param name="dropDownListId">The id of the form field to return</param>
        /// <param name="listType">automaticaly add the javascript for a type of list</param>
        /// <returns>An HTML select element with an option subelement for each item in the list</returns>
        private static MvcHtmlString CascadingDropDownList(HtmlHelper htmlHelper, IEnumerable <CascadingSelectListItem> selectList, string optionLabel, MvcHtmlString mvcHtmlString, string dropDownListId, CascadingDropDownListType listType)
        {
            XDocument selectDoc      = XDocument.Parse(mvcHtmlString.ToString());
            string    prefixDataAttr = "data-";
            string    prefixIdAttr   = "id-";
            string    separator      = "|";

            List <XElement> options = selectDoc.Element("select").Descendants().ToList();

            if (options != null && options.Any())
            {
                foreach (XElement option in options)
                {
                    CascadingSelectListItem cascadingItem = null;

                    if (option.Attributes("value").FirstOrDefault() != null)
                    {
                        cascadingItem = selectList.FirstOrDefault(x => x.Value == option.Attributes("value").First().Value);
                    }

                    if (cascadingItem != null)
                    {
                        foreach (var parentValue in cascadingItem.ParentValues)
                        {
                            option.SetAttributeValue(prefixIdAttr + parentValue.Key, parentValue.Key);
                            option.SetAttributeValue(prefixDataAttr + parentValue.Key, string.Join(separator, parentValue.Value));
                        }
                    }
                    else if (option.Value == optionLabel)
                    {
                        List <string> parentIds = selectList.SelectMany(x => x.ParentValues.Select(y => y.Key)).Distinct().ToList();
                        if (parentIds != null && parentIds.Any())
                        {
                            parentIds.Sort();
                            foreach (var parentId in parentIds)
                            {
                                List <string> parentValues = selectList.SelectMany(x => x.ParentValues.Where(z => z.Key == parentId).SelectMany(y => y.Value)).Distinct().ToList();
                                if (parentValues != null && parentValues.Any())
                                {
                                    parentValues.Sort();
                                    option.SetAttributeValue(prefixIdAttr + parentId, parentId);
                                    option.SetAttributeValue(prefixDataAttr + parentId, string.Join(separator, parentValues));
                                }
                            }
                        }
                    }
                }
            }

            if (listType == CascadingDropDownListType.ChildList)
            {
                string javascript = "<script type=\"text/javascript\">$(document).ready(function() { $(\"#" + dropDownListId + "\").cascadingDropDownList(); });</script>";
                htmlHelper.Script(javascript, HtmlHelpersScript.Priority.FastRender);
            }

            return(MvcHtmlString.Create(selectDoc.ToString()));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Create a dropdownlist whose display will depend on a parent dropdownlist
        /// </summary>
        /// <typeparam name="TModel">The type of the model</typeparam>
        /// <typeparam name="TProperty">The type of the property</typeparam>
        /// <param name="htmlHelper">The HTML helper</param>
        /// <param name="expression">An expression that identifies the object that contains the properties to display</param>
        /// <param name="selectList">A collection of System.Web.Mvc.SelectListItem objects that are used to populate the drop-down list</param>
        /// <param name="optionLabel">The text for a default empty item. This parameter can be null</param>
        /// <param name="htmlAttributes">An object that contains the HTML attributes to set for the element</param>
        /// <param name="listType">automaticaly add the javascript for a type of list</param>
        /// <returns>An HTML select element for each property in the object that is represented by the expression</returns>
        public static MvcHtmlString CascadingDropDownListFor <TModel, TProperty>(this HtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TProperty> > expression, IEnumerable <CascadingSelectListItem> selectList, string optionLabel, object htmlAttributes = null, CascadingDropDownListType listType = CascadingDropDownListType.ChildList)
        {
            MvcHtmlString mvcHtmlString  = htmlHelper.DropDownListFor(expression, selectList, optionLabel, htmlAttributes);
            string        dropDownListId = htmlHelper.IdFor(expression).ToString();

            return(CascadingDropDownList(htmlHelper, selectList, optionLabel, mvcHtmlString, dropDownListId, listType));
        }