Ejemplo n.º 1
0
        public static MvcHtmlString PartialFor <TModel, TProperty>(this HtmlHelper <TModel> helper, Expression <Func <TModel, TProperty> > expression, string partialViewName)
        {
            string name     = ExpressionHelper.GetExpressionText(expression);
            object model    = ModelMetadata.FromLambdaExpression(expression, helper.ViewData).Model;
            var    viewData = new ViewDataDictionary(helper.ViewData)
            {
                TemplateInfo = new TemplateInfo {
                    HtmlFieldPrefix = name
                }
            };

            return(helper.Partial(partialViewName, model, viewData));
        }
Ejemplo n.º 2
0
        //        public static HelpMessage HelpMessageFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, string classname)
        //        {
        //            var name = helper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(expression));
        //            var help = helper.ViewContext.ViewData["help"] as string;
        //            var m = helper.ViewData.ModelState[name];
        //            if (m == null && help == null)
        //                return new HelpMessage();
        //            var b = new TagBuilder("span");
        //            b.AddCssClass(classname);
        //            var hasError = false;
        //            if (m != null && m.Errors.Count > 0)
        //            {
        //                b.SetInnerText(m.Errors[0].ErrorMessage);
        //                hasError = true;
        //            }
        //            else if (help != null)
        //                b.InnerHtml = help;
        //            else
        //                return new HelpMessage();
        //            return new HelpMessage { message = new HtmlString(b.ToString()), errorClass = hasError ? "error" : "" };
        //        }
        public static MvcHtmlString ValidationMessageLabelFor <TModel, TProperty>(this HtmlHelper <TModel> html, Expression <Func <TModel, TProperty> > expression, string errorClass = "error")
        {
            string        elementName = ExpressionHelper.GetExpressionText(expression);
            MvcHtmlString normal      = html.ValidationMessageFor(expression);

            if (normal != null)
            {
                string newValidator = Regex.Replace(normal.ToHtmlString(), @"<span([^>]*)>([^<]*)</span>", string.Format("<label for=\"{0}\" $1>$2</label>", elementName), RegexOptions.IgnoreCase);
                if (!string.IsNullOrWhiteSpace(errorClass))
                {
                    newValidator = newValidator.Replace("field-validation-error", errorClass);
                }
                return(MvcHtmlString.Create(newValidator));
            }
            return(null);
        }
Ejemplo n.º 3
0
        public static HtmlString HiddenFor2 <TModel, TProperty>(this HtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TProperty> > expression)
        {
            var tb = new TagBuilder("input");

            tb.MergeAttribute("type", "hidden");
            var name   = ExpressionHelper.GetExpressionText(expression);
            var v      = htmlHelper.ViewData.Eval(name);
            var prefix = htmlHelper.ViewData.TemplateInfo.HtmlFieldPrefix;

            if (Util.HasValue(prefix))
            {
                name = prefix + "." + name;
            }
            tb.MergeAttribute("name", name);
            if (v != null)
            {
                tb.MergeAttribute("value", v.ToString());
            }
            else
            {
                tb.MergeAttribute("value", "");
            }
            return(new HtmlString(tb.ToString()));
        }
Ejemplo n.º 4
0
 public static string NameFor2 <TModel, TProperty>(this HtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TProperty> > expression)
 {
     return(htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(expression)));
 }
Ejemplo n.º 5
0
 public static string GetIdFor <M, P>(this M model, Expression <Func <M, P> > ex)
 {
     return(ExpressionHelper.GetExpressionText(ex).ToSuitableId());
 }
Ejemplo n.º 6
0
 public static string GetNameFor <M, P>(this M model, Expression <Func <M, P> > ex)
 {
     return(ExpressionHelper.GetExpressionText(ex));
 }
Ejemplo n.º 7
0
        public static MvcHtmlString LocalizedLabelFor <TModel, TValue>(this HtmlHelper html, Expression <Func <TModel, TValue> > expression)
        {
            var label = Localize(expression);

            return(label != null?html.Label(ExpressionHelper.GetExpressionText(expression), label.ToString()) : html.Label(ExpressionHelper.GetExpressionText(expression)));
        }
Ejemplo n.º 8
0
        public static MvcHtmlString CollectionItemClientIdFor <TModel, TValue>(this HtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TValue> > expression)
        {
            var fullName    = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(expression));
            var sanitizedId = TagBuilder.CreateSanitizedId(fullName) ?? fullName;

            return(new MvcHtmlString(
                       sanitizedId
                       .Replace(".", HtmlHelper.IdAttributeDotReplacement)
                       .Replace("[", HtmlHelper.IdAttributeDotReplacement)
                       .Replace("]", HtmlHelper.IdAttributeDotReplacement)
                       ));
        }