ASP.Net MVC – Conditional Rndering Partial Views with Action<T> Delegate

This is an update to my previous post regarding conditional rendering partial views, in that I used the internal implementation of the Html.RenderPartail(…) method to create the Html extension. Later I found a simple way to achieve the same using Action<T> delegate

<% Html.PartialIf(this.Model.Exists, html => html.RenderPartial("MyPartialView")); %>

If you look at the PartialIf implementation, it is simple, cleaner than the previous technique I have mentioned in my post.

1
2
3
4
5
6
7
public static void PartialIf(this HtmlHelper htmlHelper, bool condition, Action<HtmlHelper> action)
{
if (condition)
{
action.Invoke(htmlHelper);
}
}

That’s it :)