Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC Helpers, Merging two object htmlAttributes together

I have a situation where I need to write an HTML Helper to extend another html helper. Normally, the helper would look like this.

@Html.TextAreaFor(model => model.Content, new { @class = "some css", @data_bind = "some other stuff..." })

This works fine, but it has to be wrapped in some other HTML that is always the same. I wanted to encapsulate it for convenience, like this.

public static MvcHtmlString CondensedHelperFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object htmlAttributes) {             var stringBuilder = new System.Text.StringBuilder();             var tag = new TagBuilder("div"); tag.AddCssClass("some_css");             stringBuilder.Append(toolbar.ToString(TagRenderMode.SelfClosing));              stringBuilder.Append(htmlHelper.TextAreaFor(expression, htmlAttributes));             // more tags and such...              return new MvcHtmlString(stringBuilder.ToString());         } 

The line stringBuilder.Append(htmlHelper.TextAreaFor... is what I want to change. The CSS class that has to go there is always going to be present. So I would rather include it here. However I would like to be able to specify additional CSS classes in the top-level helper. So ...

@Html.CondensedHelperFor(model => model.Content, new { @class = "some_other_css" })

And the static css that will always be there get blanketed in through the Helper.

Any ideas?

like image 791
Ciel Avatar asked May 17 '11 23:05

Ciel


1 Answers

You might be able to do so with the standard MVC helper method.

HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes) 

htmlAttributes is a an object

like image 80
Noam Avatar answered Oct 06 '22 00:10

Noam