public static class HelperExtentions
{
private const int DefaultButtonThemeHeight = 31;
public static string SpanIf(this HtmlHelper helper, bool conditionToCheck, object htmlAttributes)
{
if (!conditionToCheck) return string.Empty;
var tagBuilder = new TagBuilder("span");
tagBuilder.MergeAttributes(EnumerateAttributePairs(htmlAttributes));
return tagBuilder.ToString(TagRenderMode.Normal);
}
public static string ThemedButton(this HtmlHelper helper, string name, string innerHtml)
{
return ThemedButton(helper, name, innerHtml, null);
}
public static string ThemedButton(this HtmlHelper helper, string name, string innerHtml, object buttonAttributes)
{
var content = new TagBuilder("div") { InnerHtml = innerHtml };
var button = new TagBuilder("button")
{
InnerHtml = content.ToString()
};
button.Attributes["type"] = "button";
button.Attributes["id"] = name;
button.MergeAttributes(EnumerateAttributePairs(buttonAttributes));
return button.ToString();
}
private static IDictionary<string, object> EnumerateAttributePairs(object htmlAttributes)
{
return htmlAttributes != null ? new RouteValueDictionary(htmlAttributes) : new RouteValueDictionary();
}
public static MvcHtmlString HiddenJsonFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)
{
return HiddenJsonFor(htmlHelper, expression, (IDictionary<string, object>)null);
}
public static MvcHtmlString HiddenJsonFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object htmlAttributes)
{
return HiddenJsonFor(htmlHelper, expression, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
}
public static MvcHtmlString HiddenJsonFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IDictionary<string, object> htmlAttributes)
{
var name = ExpressionHelper.GetExpressionText(expression);
var metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
var tagBuilder = new TagBuilder("input");
tagBuilder.MergeAttributes(htmlAttributes);
tagBuilder.MergeAttribute("name", name);
tagBuilder.MergeAttribute("type", "hidden");
var json = JsonConvert.SerializeObject(metadata.Model);
tagBuilder.MergeAttribute("value", json);
return MvcHtmlString.Create(tagBuilder.ToString());
}
}
Html Extensions to help view objects
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.