Check if (partial) view exists from HtmlHelperMethod

asp.net-mvc, c#

Solution

But you can't do the above in a helper, as you don't have access to the controller context.

Oh yes, you do have access:

public static HtmlString MyHelper(this HtmlHelper html)
{
    var controllerContext = html.ViewContext.Controller.ControllerContext;
    var result = ViewEngines.Engines.FindView(controllerContext, name, null);
    ...
}

Problem

Does anyone know if it's possible to check if a partial view exists from within an HtmlHelperExtension? I know it's possible from a controller using the following: ``` private bool ViewExists(string name) { ViewEngineResult result = ViewEngines.Engines.FindView(ControllerContext, name, null); return (result.View != null); } ``` Source: Does a View Exist in Asp.Net MVC? But you can't do the above in a helper, as you don't have access to the controller context. Any thoughts on how to do this?

Original source

Related problems