Add HtmlHelper support to a WebForms page

1. Implement IWebFormsView<ModelClass> in your codebehind for the page class.
For example:
// "Registration" is the class of the "Model" we want the HtmlHelper to be strongly-typed to
public class MyPage : System.Web.UI.Page, IWebFormsView<Registration> {
   HtmlHelper<Registration> _Html = null;
   public HtmlHelper<Registration> Html {
      get {
         if(_Html == null) {
            _Html = new HtmlHelper<Registration>(this);
         }
         return _Html;
      }
   }
}

2. Your project will need to build successfully for intellisense to be updated against the codebehind
3. You can then use HtmlHelper's like so:
<%=Html.LabelFor(model => model.FirstName) %>
<%=Html.TextBoxFor(model => model.FirstName) %>
<%=Html.ValidationMessageFor(model => model.FirstName) %>

Last edited May 18, 2012 at 11:45 PM by aikeru, version 1

Comments

No comments yet.