Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>So there at least two ways you can define helpers. Here is one way.</p> <pre><code>namespace MvcHelpers.Helpers { public class ButtonHelper { public static MvcHtmlString SpecialButton(string id) { return new MvcHtmlString(String.Format("&lt;input id=\"{0}\" type=\"button\" value=\"Special\"/&gt;", id)); } } } </code></pre> <p>I think that this is what you are referring to when you say "C# Classes" but in the reality they are just helpers also that make it easier to output HTML that will be rendered in you View.</p> <p>Here is another method for designing helpers which is probably what you are referring to as a "helper". </p> <pre><code>namespace MvcHelpers.Helpers { public static class ButtonHelperExtension { public static MvcHtmlString SpecialButton(this HtmlHelper helper, string id) { return new MvcHtmlString(String.Format("&lt;input id=\"{0}\" type=\"button\" value=\"Special\"/&gt;", id)); } } } </code></pre> <p>This is referred to as a helper extension and now we can reference this like all of the other standard HTML helpers. Here is how we reference these two type of helpers in the Razor view.</p> <pre><code>@{ ViewBag.Title = "Home"; } @using MvcHelpers.Helpers; &lt;h2&gt;Home Page&lt;/h2&gt; @ButtonHelper.SpecialButton("button1") &lt;br /&gt; @Html.SpecialButton("button2") </code></pre> <p>Both techniques achieve exactly the same results. The one referenced as Html.SpecialButton is the extension method. The only advantage I see to this is that it is clear that it is an HTML helper and not performing some other function. People that are familiar with using HTML helpers will now see your new function when they use InteliSense. But they do still have to put in the proper uses clause for it to show up. So same results with slightly different semantics.</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.
    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