Пример #1
0
        public static MvcHtmlString ValueBoxFor <TModel, TProperty>(this HtmlHelper <TModel> htmlHelper, string name, System.Linq.Expressions.Expression <System.Func <TModel, TProperty> > expression, object htmlAttributes = null)
        {
            System.Web.Routing.RouteValueDictionary htmlAttributesDict = new System.Web.Routing.RouteValueDictionary(htmlAttributes ?? new
            {
            });
            ModelMetadata metadata     = ModelMetadata.FromLambdaExpression <TModel, TProperty>(expression, htmlHelper.ViewData);
            string        propertyName = ExpressionHelper.GetExpressionText(expression);
            string        icon         = metadata.Icon();

            if (icon.IsNotNullAndEmpty())
            {
                htmlHelper.ViewData[ValueBoxHelper.ValueBox_Image_Icon] = icon;
            }
            if (metadata.DataTypeName == DataType.Password.ToString())
            {
                if (htmlAttributesDict == null)
                {
                    htmlAttributesDict = new System.Web.Routing.RouteValueDictionary();
                }
                if (htmlAttributesDict.ContainsKey("type"))
                {
                    htmlAttributesDict["type"] = "password";
                }
                else
                {
                    htmlAttributesDict.Add("type", "password");
                }
            }
            TModel model         = htmlHelper.ViewData.Model;
            object propertyValue = metadata.Model;

            if (string.IsNullOrEmpty(name))
            {
                name = propertyName;
            }
            MvcHtmlString result;

            if ((htmlHelper.ViewMode() == ViewMode.Create && !metadata.EditableWhenCreate()) || (htmlHelper.ViewMode() == ViewMode.Edit && !metadata.EditableWhenEdit()))
            {
                string text = string.Format(metadata.EditFormatString ?? "{0}", propertyValue);
                result = MvcHtmlString.Create(htmlHelper.Encode(text));
            }
            else
            {
                if (typeof(TProperty).FullName.StartsWith("System"))
                {
                    string text = string.Format(metadata.EditFormatString ?? "{0}", propertyValue);
                    result = htmlHelper.ValueBox(typeof(TProperty), name, text, htmlAttributesDict);
                }
                else
                {
                    result = htmlHelper.ValueBox(typeof(TProperty), name, propertyValue, htmlAttributesDict);
                }
            }
            return(result);
        }