Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7876977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:15:20+00:00 2026-06-03T03:15:20+00:00

Rendering a view to a string has been asked many times, however my question

  • 0

Rendering a view to a string has been asked many times, however my question differs.

The ViewEngineResult.View has a method called Render(ViewContext, TextWriter)

My code works fine if I pass in the controller which houses my email views, however if the action method originated on another controller then I need to be able to modify the ViewContext to look in the EmailControllers views.

I can’t figure out exactly what property the Render method is using to figure out what view folders to look in. Essentially i’m looking to figure that out so I can tell it to look in the Email views folder.

Here’s my code for reference:

public static string RenderPartialViewToString(Controller controller, string viewName, object model)
    {
        var oldModel = controller.ViewData.Model;
        controller.ViewData.Model = model;
        try
        {
            using (var sw = new StringWriter())
            {
                var viewResult = ViewEngines.Engines.FindView(controller.ControllerContext, viewName,
                                                                           null);

                var viewContext = new ViewContext(controller.ControllerContext, viewResult.View, controller.ViewData, controller.TempData, sw);
                viewResult.View.Render(viewContext, sw);

                controller.ViewData.Model = oldModel;
                return sw.GetStringBuilder().ToString();
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

So to recap, If I pass in the orders controller the FindView looks for my view within the Orders view folder, I need to tell it to look in the Email view folder.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T03:15:21+00:00Added an answer on June 3, 2026 at 3:15 am

    The ViewEngine looks at the RouteData to determine what .cshtml file to load. Specifically the controller and action properties. You could (and probably recommended) pass in the controller name you wish to use but you can see I’ve hard coded it to Email for now in the code below.

    public static string RenderPartialViewToString(Controller controller, string viewName, object model)
    {
        var oldModel = controller.ViewData.Model;
        controller.ViewData.Model = model;
        try
        {
            using (var sw = new StringWriter())
            {
    
                controller.RouteData.Values["controller"] = "Email";
    
                var viewResult = ViewEngines.Engines.FindView(controller.ControllerContext, viewName, null);
    
                var viewContext = new ViewContext(controller.ControllerContext, viewResult.View, controller.ViewData, controller.TempData, sw);
    
                viewResult.View.Render(viewContext, sw);
    
                controller.ViewData.Model = oldModel;
                return sw.GetStringBuilder().ToString();
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    

    More stuff if you really want to know

    Razor has several locations it looks for when trying to determine which view to use. {0} here is the action (or passed in view name) and {1} is the controller.

            ViewLocationFormats = new[] { 
                "~/Views/{1}/{0}.cshtml",
                "~/Views/{1}/{0}.vbhtml", 
                "~/Views/Shared/{0}.cshtml", 
                "~/Views/Shared/{0}.vbhtml"
            }; 
    

    Same as here except we also have {2} which is the area.

            AreaViewLocationFormats = new[] {
                "~/Areas/{2}/Views/{1}/{0}.cshtml", 
                "~/Areas/{2}/Views/{1}/{0}.vbhtml",
                "~/Areas/{2}/Views/Shared/{0}.cshtml", 
                "~/Areas/{2}/Views/Shared/{0}.vbhtml" 
            };
    

    All of these parameters come from the RouteData (unless specifically overwritten via parameter such as View("viewname"). They’re specifically action, controller and area. By modifying these values you can alter several ways MVC works. Including determining which controller to use before one is selected. Same with actions and areas.

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a portlet which has many rendering and action methods: @Controller @RequestMapping(VIEW) public
I’ve been dissecting the Orchard CMS rendering and view engine in an effort to
I know this subject has been run into the ground, however I have tried
This works, because it returns the result of partial view rendering in a string:
I need to render an ASP.NET MVC view to a string so as to
I have an action that needs to render a view to string. The view
While rendering the view page, based on some condition in the controller action I
I'm having a hard time rendering a view in Haml,using MongoMapper for a small
In an MVC3 application I have a view rendering two Ajax partial views: a
I'm trying to understand why respond_with/to is rendering the wrong view... controller respond_to :html,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.