I'm working on an old website built on Umbraco 4 and have upgraded to the latest version of V4 that has MVC-support. All the views are based on web forms so I can't really use all the Razor-goodness that comes with the MVC-support.
I know that I can create a macropartial, create a macro and insert that into my webforms views (master pages) but I would like to avoid that fiddeling in the backoffice. Is there any way to render a mvc-view (partial och macropartial) directly from the webforms aspx-files? I was thinking that an inline macro could do the job but I haven't got it to work. Basiclly tried this code: <umbraco:Macro runat="server" language="cshtml"> <p>@DateTime.Now.ToString()</p> @Html.Partial("Test") </umbraco:Macro>
But is says that the Html-object dont have a method called Partial?
public static string RenderViewToString(ControllerContext context, string viewPath, object model = null, bool partial = false)
{
// first find the ViewEngine for this view
ViewEngineResult viewEngineResult = null;
if (partial)
viewEngineResult = ViewEngines.Engines.FindPartialView(context, viewPath);
else
viewEngineResult = ViewEngines.Engines.FindView(context, viewPath, null);
if (viewEngineResult == null)
throw new FileNotFoundException("View cannot be found.");
// get the view and attach the model to view data
var view = viewEngineResult.View;
context.Controller.ViewData.Model = model;
string result = null;
using (var sw = new StringWriter())
{
var ctx = new ViewContext(context, view,
context.Controller.ViewData,
context.Controller.TempData,
sw);
view.Render(ctx, sw);
result = sw.ToString();
}
return result;
}
I used this method in my Umbraco website to fetch data from View/PartialView by passing model.
And also you can use this method in Controller as well.
Render MVC-Partial from a Inline Razor script
Hi!
I'm working on an old website built on Umbraco 4 and have upgraded to the latest version of V4 that has MVC-support. All the views are based on web forms so I can't really use all the Razor-goodness that comes with the MVC-support.
I know that I can create a macropartial, create a macro and insert that into my webforms views (master pages) but I would like to avoid that fiddeling in the backoffice. Is there any way to render a mvc-view (partial och macropartial) directly from the webforms aspx-files? I was thinking that an inline macro could do the job but I haven't got it to work. Basiclly tried this code:
<umbraco:Macro runat="server" language="cshtml">
<p>@DateTime.Now.ToString()</p>
@Html.Partial("Test")
</umbraco:Macro>
But is says that the Html-object dont have a method called Partial?
Hi Markus,
You can use below method in the Razor view.
For that you need to put that method in any common class and than you can call this method inside Razor like below.
Method :
I used this method in my Umbraco website to fetch data from View/PartialView by passing model.
And also you can use this method in Controller as well.
Hopw this helps you.
Regards,
Urvish Mandaliya
Hi!
Thank you!
But if I want to render this from a inline razor script (from the webforms implementation of razor) where would I find a ControllerContext?
is working on a reply...