Is there a way to dynamically execute am ASP.NET MVC global helper function (not HtmlHelper extension method), similar to using a reflection approach to calling a method on a class? I need to be able to invoke a method dynamically, and inject the response into a view.
Thanks.
Assuming you’re basing the decision of which helper to execute on values the view has access to, you can encapsulate each helper function in a partial view (or just refactor the helpers as partials) and render them either directly with
@Html.RenderPartial. If you want more isolation between the two views, and more flexibility, go with@Html.RenderActioninstead (but note that you’re reinvoking a medium-large part of the MVC pipeline when you invoke a child action from a view).