/// <summary>
        /// 列表控件(无限极)
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="source">绑定数据源</param>
        /// <param name="option">选项</param>
        /// <param name="name"></param>
        /// <param name="selValue">选中值</param>
        /// <returns></returns>
        public static MvcHtmlString DropDownListEx(this System.Web.Mvc.HtmlHelper htmlHelper,
                                                   string name,
                                                   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(name), source, attrs));
        }
Exemple #2
0
        public static System.Web.IHtmlString DtxLabelAndDropDownListFor <TModel, TValue>
            (this System.Web.Mvc.HtmlHelper <TModel> html,
            System.Linq.Expressions.Expression <System.Func <TModel, TValue> > expression,
            int labelColumn = 3, bool readOnly = false,
            //bool leftToRight = false,
            string optionLabel = null)
        {
            string result = string.Empty;

            // **************************************************
            string cssClass = "form-control";

            //if (leftToRight)
            //{
            //	cssClass =
            //		string.Format("{0} ltr", cssClass);
            //}
            // **************************************************

            result += "<div class=\"form-group\">";

            result +=
                DtxLabelFor(html: html, expression: expression, column: labelColumn);

            result +=
                $"<div class=\"col-sm-{ 12 - labelColumn }\">";

            // **************************************************
            System.Web.Mvc.ModelMetadata modelMetadata =
                System.Web.Mvc.ModelMetadata.FromLambdaExpression(expression, html.ViewData);

            string htmlFieldName =
                System.Web.Mvc.ExpressionHelper.GetExpressionText(expression);
            // **************************************************

            // **************************************************
            object htmlAttributes = null;

            if (readOnly)
            {
                optionLabel = null;

                // TODO: تست شود که کدامیک از دو دستور مناسب است
                //htmlAttributes =
                //	new { @class = cssClass, disabled = "disabled" };

                // TODO: تست شود که کدامیک از دو دستور مناسب است
                htmlAttributes =
                    new { @class = cssClass, @readonly = "readonly" };
            }
            else
            {
                if (modelMetadata.IsRequired == false)
                {
                    if (optionLabel == null)
                    {
                        optionLabel =
                            Resources.Messages.YouCanSelectAnItem;
                    }

                    htmlAttributes = new { @class = cssClass };
                }
                else
                {
                    if (optionLabel == null)
                    {
                        optionLabel =
                            Resources.Messages.YouShouldSelectAnItem;
                    }

                    // **************************************************
                    string errorMessage =
                        Resources.Messages.RequiredValidationErrorMessage;

                    string displayName =
                        modelMetadata.DisplayName ??
                        modelMetadata.PropertyName ??
                        htmlFieldName.Split('.').Last();

                    if (string.IsNullOrWhiteSpace(displayName) == false)
                    {
                        errorMessage =
                            string.Format(Resources.Messages.RequiredValidationErrorMessage, displayName);
                    }
                    // **************************************************

                    htmlAttributes =
                        new { @class = cssClass, data_val = "true", data_val_required = errorMessage };
                }
            }
            // **************************************************

            string dropDownList =
                html.DropDownList
                    (name: htmlFieldName, selectList: null, optionLabel: optionLabel, htmlAttributes: htmlAttributes)
                .ToHtmlString();

            result += dropDownList;

            var validationHtmlAttributes =
                new { @class = "text-danger" };

            string validationMessageFor =
                html.ValidationMessageFor(expression: expression, validationMessage: string.Empty, htmlAttributes: validationHtmlAttributes)
                .ToHtmlString();

            result += validationMessageFor;

            result += "</div>";
            result += "</div>";

            return(html.Raw(result));
        }