Home Contact

Allan Rwakatungu's blog

Developer and Enterprise Architect

News

Twitter












Archives

Post Categories

Image Galleries

Syndication:

Using strongly Typed HTML/AJAX Helpers with ASP.NET MVC

If you dont want to use strings but instead use strongly typed expression syntax in ASP.NET MVC, you can use the HtmlHelper<TModel> and AjaxHelper<TModel> types that are exposed on the ViewPage<TModel> base class.

You can then write syntax like

 <ul>
  <%foreach (Product p in ViewData.Model.Products) { %>
      <li>
          <%=Html.ActionLink<CatalogController>(x=>x.Show(p.ProductCode),p.Name) %>
      </li>
  <%} %>
  </ul>

instead of

 <ul>
  <%foreach (Product p in ViewData.Model.RelatedProducts) { %>
      <li>
        
           <%= Html.ActionLink("Product", new { Controller = "Catalog", Action="Show", Name = p.Name})%>
      </li>
  <%} %>
  </ul>

However, this feature does not come with the default ASP.NET MVC 1.0 installation , you'll have to download MVC Futures assemply from codeplex

Make sure you add the Microsoft.Web.Mvc namespace to the <namespaces> within the <pages> section of your web.config file.  This will cause the extension method to be automatically imported on all views. 
Once you do this and compile, your good to go


Tuesday, October 06, 2009 11:42 AM

Feedback

No comments posted yet.


Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: