Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>All of the basic html helpers return an object of type <code>System.Web.Mvc.MvcHtmlString</code>. You can set up extension methods for that class. Here is an example:</p> <pre><code>public static class MvcHtmlStringExtensions { public static MvcHtmlString If(this MvcHtmlString value, bool check) { if (check) { return value; } return null; } public static MvcHtmlString Else(this MvcHtmlString value, MvcHtmlString alternate) { if (value == null) { return alternate; } return value; } } </code></pre> <p>Then you can use these in a view like:</p> <pre><code>@Html.TextBoxFor(model =&gt; model.Name) .If(Model.Name.StartsWith("A")) .Else(Html.TextBoxFor(model =&gt; model.LastName) </code></pre> <p>To make extension methods that modify attributes on the rendered HTML tag, you'll have to convert the result to a string, and find and replace the value you're looking for.</p> <pre><code>using System.Text.RegularExpressions; public static MvcHtmlString Identity(this MvcHtmlString value, string id) { string input = value.ToString(); string pattern = @"(?&lt;=\bid=")[^"]*"; string newValue = Regex.Replace(input, pattern, id); return new MvcHtmlString(newValue); } public static MvcHtmlString Name(this MvcHtmlString value, string id) { string input = value.ToString(); string pattern = @"(?&lt;=\bname=")[^"]*"; string newValue = Regex.Replace(input, pattern, id); return new MvcHtmlString(newValue); } </code></pre> <p>The <code>id</code> and <code>name</code> attributes are always added by the html helpers, but if you want to work with attributes that may not be there (and you'll have to add them instead of just replacing them), you'll need to modify the code.</p>
    singulars
    1. This table or related slice is empty.
    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.
    3. VO
      singulars
      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