public static MvcHtmlString BootstrapEditorFor <TModel, TValue>(this HtmlHelper <TModel> helper, Expression <Func <TModel, TValue> > expression)
        {
            MvcHtmlString label      = LabelExtensions.LabelFor(helper, expression, new { @class = "control-label col-xs-12 col-sm-4 col-md-3" });
            MvcHtmlString editor     = EditorExtensions.EditorFor(helper, expression, new { htmlAttributes = new { @class = "form-control" } });
            MvcHtmlString validation = ValidationExtensions.ValidationMessageFor(helper, expression, null, new { @class = "text-danger" });
            // Build the input elements
            TagBuilder editorDiv = new TagBuilder("div");

            editorDiv.AddCssClass("col-xs-4 col-sm-2 col-md-2 col-lg-1");
            editorDiv.InnerHtml = editor.ToString();
            // Build the validation message elements
            TagBuilder validationSpan = new TagBuilder("span");

            validationSpan.AddCssClass("help-block");
            validationSpan.InnerHtml = validation.ToString();
            TagBuilder validationDiv = new TagBuilder("div");

            validationDiv.AddCssClass("col-xs-12 col-md-8 col-sm-offset-4 col-md-offset-3");
            validationDiv.InnerHtml = validationSpan.ToString();
            // Combine all elements
            StringBuilder html = new StringBuilder();

            html.Append(label.ToString());
            html.Append(editorDiv.ToString());
            html.Append(validationDiv.ToString());
            // Build the outer div
            TagBuilder outerDiv = new TagBuilder("div");

            outerDiv.AddCssClass("form-group");
            outerDiv.InnerHtml = html.ToString();
            return(MvcHtmlString.Create(outerDiv.ToString()));
        }
Example #2
0
        public static MvcHtmlString EditorFor <TModel, TValue>(this HtmlHelper <TModel> html,
                                                               Expression <Func <TModel, TValue> > expression,
                                                               bool useEnumTemplate, string templatename, object additionalViewData)
        {
            RouteValueDictionary vd = null;
            var style = "";

            if (additionalViewData != null)
            {
                vd    = new RouteValueDictionary(additionalViewData);
                style = (string)vd["Style"];
            }
            if (typeof(TValue).IsEnum)
            {
                return(EditorExtensions.EditorFor(html, expression, templatename, new { EnumType = typeof(TValue), Style = style, AdditionalRVD = vd }));
            }
            else if (Nullable.GetUnderlyingType(typeof(TValue)) != null &&
                     Nullable.GetUnderlyingType(typeof(TValue)).IsEnum)
            {
                return(EditorExtensions.EditorFor(html, expression, templatename,
                                                  new { EnumType = Nullable.GetUnderlyingType(typeof(TValue)), Style = style, AdditionalRVD = vd }));
            }

            return(EditorExtensions.EditorFor(html, expression, templatename, additionalViewData));
        }
Example #3
0
        public static MvcHtmlString PhoneEditorFor <TModel, TValue>(this HtmlHelper <TModel> helper, Expression <Func <TModel, TValue> > expression)
        {
            MvcHtmlString editor = EditorExtensions.EditorFor(helper, expression,
                                                              new { htmlAttributes = new { @class = "form-control" } });

            TagBuilder inputTableCell = new TagBuilder("div");

            inputTableCell.MergeAttribute("style", "display:table-cell;");
            inputTableCell.InnerHtml = editor.ToString()
                                       .Replace("class=\"form-control text-box single-line\"", "style=\"border:0;\"");

            TagBuilder prefixDiv = new TagBuilder("div");

            prefixDiv.MergeAttribute("style", "display:table-cell;width:28px;padding-top:5px;");
            prefixDiv.InnerHtml = "38 0";

            TagBuilder phoneFormControl = new TagBuilder("div");

            phoneFormControl.AddCssClass("form-control text-box single-line");
            phoneFormControl.MergeAttribute("style", "padding:0 0 0 12px;max-width: 280px;");
            phoneFormControl.InnerHtml = prefixDiv.ToString() + inputTableCell.ToString();

            StringBuilder html = new StringBuilder();

            html.Append(phoneFormControl.ToString());

            return(MvcHtmlString.Create(html.ToString()));
        }
Example #4
0
        public static MvcHtmlString EditorFor <TModel, TValue>(this HtmlHelper <TModel> html, Expression <Func <TModel, TValue> > expression, string templateName,
                                                               string idAttributeValue, object viewData)
        {
            var mvcHtmlString = EditorExtensions.EditorFor(html, expression, templateName, idAttributeValue, viewData).ToString();

            mvcHtmlString = ReplaceAttribute(mvcHtmlString, "id", idAttributeValue);
            return(new MvcHtmlString(mvcHtmlString));
        }
 public static MvcHtmlString EditorFor <TModel, TValue>(
     this HtmlHelper <TModel> htmlHelper,
     Expression <Func <TModel, TValue> > expression,
     string templateName  = null,
     string htmlFieldName = null
     )
 {
     return(EditorExtensions.EditorFor(htmlHelper, expression, templateName, htmlFieldName));
 }
Example #6
0
        public static MvcHtmlString EditorFor <TModel, TValue>(this HtmlHelper <TModel> html,
                                                               Expression <Func <TModel, TValue> > expression,
                                                               bool useEnumTemplate, string templatename)
        {
            if (typeof(TValue).IsEnum)
            {
                return(EditorExtensions.EditorFor(html, expression, templatename, new { EnumType = typeof(TValue) }));
            }
            else if (Nullable.GetUnderlyingType(typeof(TValue)) != null &&
                     Nullable.GetUnderlyingType(typeof(TValue)).IsEnum)
            {
                return(EditorExtensions.EditorFor(html, expression, templatename,
                                                  new { EnumType = Nullable.GetUnderlyingType(typeof(TValue)) }));
            }

            return(EditorExtensions.EditorFor(html, expression, templatename));
        }
        public static MvcHtmlString FieldFor <TModel, TValue>(this HtmlHelper <TModel> helper, Expression <Func <TModel, TValue> > expression)
        {
            MvcHtmlString label  = LabelExtensions.LabelFor(helper, expression, new { @class = "control-label col-md-2" });
            MvcHtmlString editor = EditorExtensions.EditorFor(helper, expression, new { htmlAttributes = new { @class = "form-control" } })
                                   MvcHtmlString validation = ValidationExtensions.ValidationMessageFor(helper, expression, null, new { @class = "text-danger" });
            StringBuilder html = new StringBuilder();

            html.Append(editor);
            html.Append(validation);
            TagBuilder innerDiv = new TagBuilder("div");

            innerDiv.AddCssClass("col-md-10");
            innerDiv.InnerHtml = html.ToString();
            html = new StringBuilder();
            html.Append(label);
            html.Append(innerDiv.ToString());
            TagBuilder outerDiv = new TagBuilder("div");

            outerDiv.AddCssClass("form-group");
            outerDiv.InnerHtml = html.ToString();
            return(MvcHtmlString.Create(outerDiv.ToString()));
        }
        public static MvcHtmlString ChangeableFor <TModel, TValue, TType>(this HtmlHelper <TModel> html, Expression <Func <TModel, TValue> > expression, Changeable <TType> changeable)
        {
            // helper to take our Changeable<T> property, and create lambdas to get
            var controller = html.ViewContext.Controller;
            var actionName = controller.ValueProvider.GetValue("action").RawValue.ToString();
            var allMethods = controller.GetType().GetMethods();
            var methods    = allMethods.Where(m => m.Name == actionName);

            foreach (var method in methods)
            {
                var attributes = method.GetCustomAttributes(true);
                foreach (var attribute in attributes)
                {
                    if (attribute.GetType().Equals(typeof(HttpPostAttribute)))
                    {
                        var a = attribute;
                    }
                }
            }
            var name = ExpressionHelper.GetExpressionText(expression);

            if (String.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name", "Name cannot be null");
            }

            // get the metadata for our Changeable<T> property
            var metadata      = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
            var type          = metadata.ModelType;
            var containerType = metadata.ContainerType;

            // create a lambda expression to get the value of our Changeable<T>
            var        arg  = Expression.Parameter(containerType, "x");
            Expression expr = arg;

            expr = Expression.Property(expr, name);
            expr = Expression.Property(expr, "Value");
            var funcExpr           = Expression.Lambda(expr, arg) as Expression <Func <TModel, TType> >;
            var valueModelMetadata = ModelMetadata.FromLambdaExpression(funcExpr, html.ViewData);

            // create a lambda expression to get whether our Changeable<T> has changed
            Expression exprChanged = arg;

            exprChanged = Expression.Property(exprChanged, name);
            exprChanged = Expression.Property(exprChanged, "Changed");
            var funcExprChanged = Expression.Lambda(exprChanged, arg) as Expression <Func <TModel, bool> >;

            // use a stringbuilder for our HTML markup
            // return label, checkbox, hidden input, EDITOR, and validation message field
            // NOTE:  this is over-simplified markup!
            // ALSO NOTE:  the EditorFor is passed the strongly typed model taken from our T.  Bonus!
            var htmlSb = new StringBuilder("\n");

            htmlSb.Append(LabelExtensions.Label(html, metadata.GetDisplayName()));
            htmlSb.Append("<br />\n");
            htmlSb.Append(InputExtensions.CheckBoxFor(html, funcExprChanged));
            htmlSb.Append(" Changed<br />\n");
            htmlSb.Append(InputExtensions.Hidden(html, name + ".OldValue", valueModelMetadata.Model) + "\n");
            htmlSb.Append(EditorExtensions.EditorFor(html, funcExpr, new KeyValuePair <string, object>("parentMetadata", metadata))); //new object[] { "parentMetadata", metadata }));
            htmlSb.Append(ValidationExtensions.ValidationMessageFor(html, funcExpr));
            htmlSb.Append("<br />\n");

            return(new MvcHtmlString(htmlSb.ToString()));
        }
Example #9
0
 public static MvcHtmlString EditorForNew <TModel, TValue>(this HtmlHelper <TModel> html, Expression <Func <TModel, TValue> > expression, string editorName, object additionalViewData = null)
 {
     return(EditorExtensions.EditorFor(html, expression, "name", editorName, additionalViewData));
 }
Example #10
0
 public static MvcHtmlString TestEditorForTestEditorForTestEditorFor <TModel, TValue>(this HtmlHelper <TModel> html, Expression <Func <TModel, TValue> > expression, string templateName)
 {
     return(EditorExtensions.EditorFor(html, expression, templateName));
 }
Example #11
0
 public static MvcHtmlString TestEditorFor <TModel, TValue>(this HtmlHelper <TModel> html, Expression <Func <TModel, TValue> > expression, string templateName, string htmlFieldName, object additionalViewData)
 {
     return(EditorExtensions.EditorFor(html, expression, templateName, htmlFieldName, additionalViewData));
 }