Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Markus Johansson 1937 posts 5865 karma points MVP 2x c-trib
    May 05, 2015 @ 12:32
    Markus Johansson
    0

    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?

  • Urvish 252 posts 776 karma points
    May 05, 2015 @ 12:48
    Urvish
    0

    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.

    @CommonClass.RenderViewToString(context,path,model,bool partial)
    

    Method :

    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.

    Hopw this helps you.

    Regards,

    Urvish Mandaliya

  • Markus Johansson 1937 posts 5865 karma points MVP 2x c-trib
    May 08, 2015 @ 14:47
    Markus Johansson
    0

    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? 

Please Sign in or register to post replies

Write your reply to:

Draft