Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET MVC Helpers, Merging two object htmlAttributes together
    primarykey
    data
    text
    <p>I have a situation where I need to write an HTML Helper to <strong>extend</strong> another html helper. Normally, the helper would look like this.</p> <p><code>@Html.TextAreaFor(model =&gt; model.Content, new { @class = "some css", @data_bind = "some other stuff..." })</code></p> <p>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.</p> <pre><code>public static MvcHtmlString CondensedHelperFor&lt;TModel, TProperty&gt;(this HtmlHelper&lt;TModel&gt; htmlHelper, Expression&lt;Func&lt;TModel, TProperty&gt;&gt; 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()); } </code></pre> <p>The line <code>stringBuilder.Append(htmlHelper.TextAreaFor...</code> is what I want to change. The CSS class that has to go there is <strong>always</strong> 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 ...</p> <p><code>@Html.CondensedHelperFor(model =&gt; model.Content, new { @class = "some_other_css" })</code></p> <p>And the static css that will always be there get blanketed in through the Helper. </p> <p>Any ideas?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload