I think you want HtmlHelper.Action
, not RenderAction
. RenderAction
writes content to the response stream. Action
returns it as a string.
http://msdn.microsoft.com/en-us/library/ee721266(v=vs.108).aspx
The action is defined in the ChildActionExtensions
class, which lives in the System.Web.Mvc.Html
, so your controller code will need to use this namespace.
Action
returns Html as a string, while RenderAction
returns void, because RenderAction
writes its Html output directly to the current response stream embedded in the parent document. This is why it works in terms of, but not with, the controller.
To create an instance of HtmlHelper in MVC 4, you can use this:
HtmlHelper helper = new HtmlHelper(new ViewContext(ControllerContext, new WebFormView(ControllerContext, "Index"), new ViewDataDictionary(), new TempDataDictionary(), new StringWriter()), new ViewPage());
As other commentators on related SO issues have said, this is not as intended (which is obvious due to the hacker way you have to install the HtmlHelper). I am not sure if this is much better than your alternative solution. It is better to refactor to be more MVC, as if you could, although sometimes I understand that this is not an option.
source share