enable MVC 3 in umbraco 4.7.2 - problem with Html.RenderAction from Macros
Hi
I'm porting from Um5 to Um4.7.2. We have a lot of data that is accessed from umbraco macros using Html.RenderAction. I'm trying to get the same principle to work in Um4.7.2.
Setup - standard umbraco + changed web.config to add MVC3. (not mvc bridge)
I would then like that the output of renderaction is put in as content on the template.
I have a controller Home:
public class HomeController : Controller
{
public ActionResult I()
{
return View();
}
[ChildActionOnly]
public ActionResult Child()
{
return Content("from render action");
}
}
If I access the action 'I' directly, this works. Meaning directly onto the Controller without Umbraco. In the view it calls
Html.RenderAction("Child","Home");
If the route goes via Umbraco and from the razor script call
Html.RenderAction("Child","Home");
I receive
Error loading Razor Script RazorRenderScript.cshtml Context Must Implement System.Web.WebPages.WebPage
Is there any reason Html.RenderAction should not be callable from with umbraco rendered razor script to call the MVC setup that is running in the same setup?
So part of the problem was that the umbraco rendered view was inheriting from WebViewPage. That was wrong.
Now the umbraco rendered view has an Umbraco provided HtmlHelper instance and a set of extension methods.
The error in the output is:
Error loading Razor Script RazorRenderScript.cshtml c:\projects\umbraco472MVC\WebApplication2\macroScripts\RazorRenderScript.cshtml(66): error CS1928: 'System.Web.WebPages.Html.HtmlHelper' does not contain a definition for 'RenderAction' and the best extension method overload 'System.Web.Mvc.Html.ChildActionExtensions.RenderAction(System.Web.Mvc.HtmlHelper, string, object)' has some invalid arguments
I cannot seem to actually hit any RenderAction eventhough I change the paramerters to e.g. ("ksdf",null).
The problem seems to be that the HtmlHelper with the extension methods comes from System.Web.MVC and the htmlHelper provided is from System.Web.WebPages.
Does anybody know how to get a working HtmlHelper from the MVC namespace?
I have tried to instanitate but are missing a context view et other things.
Not concidering myself as an expert I've tried to do the same. It would be awsome if we could combine MVC child actions and partial views into the umbraco razor macro engine.
However if only it was just this easy....
As you said RenderAction is an extension method for System.Web.Mvc.HtmlHelper, while Helper in umbraco macroscript view is an instance of System.Web.WebPages.HtmlHelper, which are two that have the same name and designed to do the same, yet are totally independent. In order to instantiate a System.Web.Mvc.HtmlHelper you need to have an instance of System.Web.Mvc.ViewContext as well as of System.Web.Mvc.IViewDataContainer. While as I said I am not an expert, it seems that this is hopeless. The viewcontext of the umbraco macroscript is of type WebPage. So I think there is no direct way to Render a child action or partial view from a typical umbraco 4 macro.
It's by no means hopeless, though it's a little fiddly to provide the context that MVC expects. I've got some helper methods in my own sites which I'm in the process of packaging up, but for now you can grab the binary from https://dl.dropbox.com/u/5582629/MvcTest.dll to test them and see how they work for you It currently supports:
@MvcTest.MvcHelpers.Partial("ViewName", model)
@MvcTest.MvcHelpers.Action("ActionName", "ControllerName", new { parameter = "value" } )
@MvcTest.MvcHelpers.Macro("MacroAlias", new { parameter = "value" })
If you want to pass the current page as the model to your partial view and don't have a DynamicNode handy to pass, you can get one with
@MvcTest.MvcHelpers.Model
You can also access instances of MVC's HtmlHelper and UrlHelper directly with:
@MvcTest.MvcHelpers.Html
@MvcTest.MvcHelpers.Url
but not everything will work. In particular, any method that renders output directly rather than returning a string won't work. That's why I'm using Partial, Action, and Macro above rather than RenderPartial, RenderAction, and RenderMacro.
For this weekend backporting from um5->um4 we are going with the MvcBridge package. It enables rendering controller output directly in razor pages and works as macro for normal asp forms pages. The controllers is also available directly on url basis so the Ajax retrieval of data is easy to port.
The only thing is the controllers has to inherit a spcecial controller and the views has to inherit a special view (stil generic with the model, so it is not too bad).
At least this enables us to port a big portion of the UI directly.
enable MVC 3 in umbraco 4.7.2 - problem with Html.RenderAction from Macros
Hi
I'm porting from Um5 to Um4.7.2. We have a lot of data that is accessed from umbraco macros using Html.RenderAction. I'm trying to get the same principle to work in Um4.7.2.
Setup - standard umbraco + changed web.config to add MVC3. (not mvc bridge)
In the umbraco I have made a template so that
Template->render razor macro->Html.RenderAction("action","controller").
I would then like that the output of renderaction is put in as content on the template.
I have a controller Home:
If I access the action 'I' directly, this works. Meaning directly onto the Controller without Umbraco. In the view it calls
If the route goes via Umbraco and from the razor script call
I receive
Error loading Razor Script RazorRenderScript.cshtml
Context Must Implement System.Web.WebPages.WebPage
Is there any reason Html.RenderAction should not be callable from with umbraco rendered razor script to call the MVC setup that is running in the same setup?
The umbraco rendered razor view:
@inherits System.Web.Mvc.WebViewPage @using System.Web.Mvc.Html <html> <body> @{ Html.RenderAction("Child","Home"); } from umbraco razor </body> </html>
Cheers
Kim
So part of the problem was that the umbraco rendered view was inheriting from WebViewPage. That was wrong.
Now the umbraco rendered view has an Umbraco provided HtmlHelper instance and a set of extension methods.
The error in the output is:
Error loading Razor Script RazorRenderScript.cshtml
c:\projects\umbraco472MVC\WebApplication2\macroScripts\RazorRenderScript.cshtml(66): error CS1928: 'System.Web.WebPages.Html.HtmlHelper' does not contain a definition for 'RenderAction' and the best extension method overload 'System.Web.Mvc.Html.ChildActionExtensions.RenderAction(System.Web.Mvc.HtmlHelper, string, object)' has some invalid arguments
I cannot seem to actually hit any RenderAction eventhough I change the paramerters to e.g. ("ksdf",null).
The problem seems to be that the HtmlHelper with the extension methods comes from System.Web.MVC and the htmlHelper provided is from System.Web.WebPages.
Does anybody know how to get a working HtmlHelper from the MVC namespace?
I have tried to instanitate but are missing a context view et other things.
Hi Kim,
Not concidering myself as an expert I've tried to do the same. It would be awsome if we could combine MVC child actions and partial views into the umbraco razor macro engine.
However if only it was just this easy....
As you said RenderAction is an extension method for System.Web.Mvc.HtmlHelper, while Helper in umbraco macroscript view is an instance of System.Web.WebPages.HtmlHelper, which are two that have the same name and designed to do the same, yet are totally independent. In order to instantiate a System.Web.Mvc.HtmlHelper you need to have an instance of System.Web.Mvc.ViewContext as well as of System.Web.Mvc.IViewDataContainer. While as I said I am not an expert, it seems that this is hopeless. The viewcontext of the umbraco macroscript is of type WebPage. So I think there is no direct way to Render a child action or partial view from a typical umbraco 4 macro.
Doron
It's by no means hopeless, though it's a little fiddly to provide the context that MVC expects. I've got some helper methods in my own sites which I'm in the process of packaging up, but for now you can grab the binary from https://dl.dropbox.com/u/5582629/MvcTest.dll to test them and see how they work for you It currently supports:
If you want to pass the current page as the model to your partial view and don't have a DynamicNode handy to pass, you can get one with
You can also access instances of MVC's HtmlHelper and UrlHelper directly with:
but not everything will work. In particular, any method that renders output directly rather than returning a string won't work. That's why I'm using Partial, Action, and Macro above rather than RenderPartial, RenderAction, and RenderMacro.
Steve
For this weekend backporting from um5->um4 we are going with the MvcBridge package. It enables rendering controller output directly in razor pages and works as macro for normal asp forms pages. The controllers is also available directly on url basis so the Ajax retrieval of data is easy to port.
The only thing is the controllers has to inherit a spcecial controller and the views has to inherit a special view (stil generic with the model, so it is not too bad).
At least this enables us to port a big portion of the UI directly.
Cheers
Kim
is working on a reply...