Monday, August 15, 2011

App_Code helpers and "Helpers" folder helpers

App_Code helpers should like this:

@FormHelpers.Whatever("foo");

 
 

In this case there will be a FormHelpers.cshtml file in the App_Code folder and it will have inside of it something like:

@helper Whatever(string css = ""){

   <input type="submit" value="submit" class="@css" />

}

 
 

There may be many little items like this collected in FormHelpers.cshtml.

A "Helpers" folder helper looks more like this.

using System.Web;

using System.Text;

public static class MyHtmlHelper

{

   public static IHtmlString Whatever(string css)

   {

      var sb = new StringBuilder();

      sb.Append("<input type='submit' value='submit' class='");

      sb.Append(css);

      sb.Append("' />");

      return new HtmlString(sb.ToString());

   }

}

 
 

One calls a "Helpers" folder helper like so:

@Html.Whatever(foo)

No comments:

Post a Comment