Exemple #1
0
        public static System.Web.IHtmlString DtxLabelAndEditorFor <TModel, TValue>
            (this System.Web.Mvc.HtmlHelper <TModel> html,
            System.Linq.Expressions.Expression <System.Func <TModel, TValue> > expression,
            int labelColumn = 3, bool readOnly = false,
            //object additionalViewData = null,
            int hintColumn = 0, string hint = null
            //bool leftToRight = false
            )
        {
            string result = string.Empty;

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

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

            bool isBoolean = false;

            if (modelMetadata.ModelType == typeof(bool))
            {
                isBoolean = true;
            }

            //if (leftToRight)
            //{
            //	editorAttributes.Add("class", "ltr");
            //}

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

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

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

            object htmlAttributes = null;

            if (readOnly == false)
            {
                if (isBoolean == false)
                {
                    htmlAttributes =
                        new
                    {
                        @class = "form-control",
                    };
                }
            }
            else
            {
                if (isBoolean == false)
                {
                    htmlAttributes =
                        new
                    {
                        @readonly = "readonly",
                        @class    = "form-control",
                    };
                }
                else
                {
                    htmlAttributes =
                        new
                    {
                        @disabled = "disabled",
                    };
                }
            }

            var viewData = new { htmlAttributes };

            // دستور ذیل در استانداردهای مایکروسافت وجود ندارد ولی در گوشی مبایل خیلی جذاب دیده نمی‌شود
            //if (isBoolean)
            //{
            //	result += "<div class=\"checkbox\">";
            //}

            string editorFor =
                html.EditorFor(expression: expression, additionalViewData: viewData)
                .ToHtmlString();

            result += editorFor;

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

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

            result += validationMessageFor;

            // دستور ذیل در استانداردهای مایکروسافت وجود ندارد ولی در گوشی مبایل خیلی جذاب دیده نمی‌شود
            //if (isBoolean)
            //{
            //	result += "</div>";
            //}

            result += "</div>";

            if (hintColumn > 0)
            {
                result +=
                    DtxLabel(html, labelText: hint, column: hintColumn);

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

                //if (string.IsNullOrWhiteSpace(hint) == false)
                //{
                //	result += hint.Trim();
                //}

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

            result += "</div>";

            return(html.Raw(result));
        }