Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As @AaronShockley says, because <code>TextBoxFor()</code> returns an <code>MvcHtmlString</code>, your only option for developing a 'fluid API' style of amending the output would be to operate on the <code>MvcHtmlString</code>s returned by the helper methods. A slightly different way of doing this which I think approaches what you're after would be to use a 'property builder' object, like this:</p> <pre><code>public class MvcInputBuilder { public int Id { get; set; } public string Class { get; set; } } </code></pre> <p>...and to set up extension methods like this:</p> <pre><code>public static MvcHtmlString TextBoxFor&lt;TModel, TProp&gt;( this HtmlHelper&lt;TModel&gt; htmlHelper, Expression&lt;Func&lt;TModel, TProp&gt;&gt; expression, params Action&lt;MvcInputBuilder&gt;[] propertySetters) { MvcInputBuilder builder = new MvcInputBuilder(); foreach (var propertySetter in propertySetters) { propertySetter.Invoke(builder); } var properties = new RouteValueDictionary(builder) .Select(kvp =&gt; kvp) .Where(kvp =&gt; kvp.Value != null) .ToDictionary(kvp =&gt; kvp.Key, kvp =&gt; kvp.Value); return htmlHelper.TextBoxFor(expression, properties); } </code></pre> <p>You can then do stuff like this in your View:</p> <pre><code>@this.Html.TextBoxFor( model =&gt; model.Name, p =&gt; p.Id = 7, p =&gt; p.Class = "my-class") </code></pre> <p>This gives you strong typing and intellisense for input properties, which you could customise for each extension method by adding properties to an appropriate MvcInputBuilder subclass.</p>
 

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