Geeks With Blogs
Allan Rwakatungu's blog Developer and Enterprise Architect

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

Posted on Tuesday, October 6, 2009 11:42 AM | Back to top


Comments on this post: Using strongly Typed HTML/AJAX Helpers with ASP.NET MVC

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Allan Rwakatungu | Powered by: GeeksWithBlogs.net