Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm not sure what your concerns are with adding your own extension methods -- why you'd have to "create your own base view page and completely take over." You can call your custom helpers in any page just as you would the built-in helpers:</p> <pre><code>@Html.TextBoxFor(x =&gt; x.Name) @Html.MyTextBoxFor(x =&gt; x.Name) </code></pre> <p>Furthermore, you can add some sort of flag parameter to your method to control whether it just executes the default functionality or something custom.</p> <p>When you create your own extension methods, you'll have to either change the signature or the name of the method.</p> <p>I used to use unique names, but ultimately found that I really wanted to be able to quickly discern my own implementations from the default, so I sometimes use:</p> <pre><code>@Html.Custom().TextBoxFor(… @Html.Custom().TextAreaFor(… </code></pre> <p>Basically, you create one new extension method that takes an <code>HtmlHelper&lt;T&gt;</code> and returns a <code>CustomHelpers&lt;T&gt;</code>.</p> <pre><code> public static CustomHelpers&lt;TModel&gt; Custom&lt;TModel&gt;(this HtmlHelper&lt;TModel&gt; html) { return new CustomHelpers&lt;TModel&gt;(html); } </code></pre> <p>The <code>CustomHelpers&lt;T&gt;</code> class defines all of your own implementations:</p> <pre><code> public class CustomHelpers&lt;TModel&gt; { private readonly HtmlHelper&lt;TModel&gt; _html; public CustomHelpers(HtmlHelper&lt;TModel&gt; html) { _html = html; } public MvcHtmlString TextBoxFor&lt;TProperty&gt;(Expression&lt;Func&lt;TModel, TProperty&gt;&gt; expression) { // because you have a reference to the "native" HtmlHelper&lt;TModel&gt;, you // can use it here and extend or modify the result, almost like a decorator; // you can get the "native" result by calling _html.TextBoxFor(expression) } </code></pre> <p>So, your "override" of <code>TextBoxFor</code> can receive a flag from your partial view to determine whether it returns the native result or something specific to the context.</p> <p>Again, the <code>CustomHelpers&lt;T&gt;</code> class is entirely optional. You'll be adding a flag parameter or something similar to the signature of your custom helpers, so you won't collide with existing helpers.</p> <p>The benefit it confers is to potentially namespace your helpers. You could have:</p> <pre><code>@Html.TextBoxFor(… @Html.JQuery().TextBoxFor(… @Html.Mobile().TextBoxFor(… </code></pre>
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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