Beispiel #1
0
        private static IEnumerable <SelectListItem> CreateSelectListItem(
            DataTable source,
            DropDownListOption option,
            string selValue = null)
        {
            var isBindValue    = selValue != null;
            var selectListItem = new List <SelectListItem>
            {
                new SelectListItem()
                {
                    Selected = true,
                    Text     = option.TopTitle,
                    Value    = ""
                }
            };

            if (source != null && source.Rows.Count > 0)
            {
                var drs = source.Select(string.Format("{0} = {1}", option.BaseIdColumnsName, option.TopParentID));

                foreach (DataRow dr in drs)
                {
                    //获取ID
                    var id = dr[option.IdColumnsName].ToString();
                    //获取类型名称
                    var typeName = dr[option.DisplayColumnsName].ToString();
                    //顶级分类显示形式
                    typeName = option.TopParentStyle + typeName;
                    //绑定数据
                    if (isBindValue && id == selValue)
                    {
                        selectListItem.Add(new SelectListItem()
                        {
                            Text = typeName, Value = id, Selected = true
                        });
                    }
                    else
                    {
                        selectListItem.Add(new SelectListItem()
                        {
                            Text = typeName, Value = id
                        });
                    }
                    var nowID = -1;
                    int.TryParse(id, out nowID);
                    if (nowID > 0)
                    {
                        BindNode(selectListItem, source, nowID, option, option.Subkey, isBindValue, selValue);
                    }
                }
            }
            return(selectListItem);
        }
Beispiel #2
0
        /// <summary>
        /// 绑定子分类
        /// </summary>
        private static void BindNode(List <SelectListItem> e,
                                     DataTable source,
                                     int topParentID,
                                     DropDownListOption option,
                                     string blank,
                                     bool isBindValue,
                                     string selValue = null
                                     )
        {
            var drs = source.Select(string.Format("{0}= {1}", option.BaseIdColumnsName.Trim(), topParentID));

            foreach (var dr in drs)
            {
                //获取ID
                var id = dr[option.IdColumnsName].ToString();
                //获取类型名称
                var typeName = dr[option.DisplayColumnsName].ToString();
                //顶级分类显示形式
                typeName = blank + typeName;

                if (isBindValue && id == selValue)
                {
                    e.Add(new SelectListItem()
                    {
                        Text = typeName, Value = id, Selected = true
                    });
                }
                else
                {
                    e.Add(new SelectListItem()
                    {
                        Text = typeName, Value = id
                    });
                }

                // 设置下一位样式
                var nextBlank = blank + "--";
                var nowID     = -1;
                int.TryParse(id, out nowID);
                if (nowID > 0)
                {
                    BindNode(e, source, nowID, option, nextBlank, isBindValue, selValue);
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// 列表控件
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <typeparam name="TProperty"></typeparam>
        /// <param name="htmlHelper"></param>
        /// <param name="expression"></param>
        /// <param name="source"></param>
        /// <param name="option"></param>
        /// <param name="selValue"></param>
        /// <returns></returns>
        public static MvcHtmlString DropDownListExFor <TModel, TProperty>(
            this HtmlHelper <TModel> htmlHelper,
            Expression <Func <TModel, TProperty> > expression,
            IList <SelectListItem> source,
            DropDownListOption option,
            string selValue       = null,
            object htmlAttributes = null
            )
        {
            var attrs = ((IDictionary <string, object>) new RouteValueDictionary(htmlAttributes));


            if (!string.IsNullOrEmpty(option.Description))
            {
                attrs.Add("description", option.Description);
            }
            if (!string.IsNullOrEmpty(option.FocusMsg))
            {
                attrs.Add("focusMsg", option.FocusMsg);
            }

            if (option.IsRequired)
            {
                attrs.Add("isEmpty", "false");
                if (!string.IsNullOrEmpty(option.EmptyMsg))
                {
                    attrs.Add("emptyMsg", option.EmptyMsg);
                }
                else
                {
                    attrs.Add("emptyMsg", "请选择选项");
                }
            }
            else
            {
                attrs.Add("isEmpty", "true");
            }
            attrs.Add("validator", "true");
            return(htmlHelper.DropDownList(ExpressionHelper.GetExpressionText(expression), source, attrs));
        }