Exemple #1
0
        public static System.Web.IHtmlString DtxLabelAndEnumDropDownListFor <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.EnumDropDownListFor
                    (expression: expression, 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));
        }