SSW Foursquare

Do you use Html Helpers and Partial Views to simplify Views?

Last updated by Brook Jeynes [SSW] 10 months ago.See history

Repeated sections of User Interface should be encapsulated in either Html Helpers or Partial Views to avoid repetition.

<div class="featured">
    @if (ViewData.ContainsKey("FeaturedProduct"))
        <span class="ProductName">@ViewBag.FeaturedProduct.Name</span>
        <span class="ProductPrice">@ViewBag.FeaturedProduct.Price</span>

Figure: Bad example – The above code could be encapsulated into a Partial View for reuse

public static class DateExtensions
    public static MvcHtmlString GetTodayDate(this System.Web.Mvc.HtmlHelper helper)
        return new MvcHtmlString(DateTime.Now.ToShortDateString());

Figure: Good example – Using an HTML Helper extension method for reusable code


Figure: Good example – Using a Partial View for reusable sections of UI

We open source. Powered by GitHub