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 7436917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:18:56+00:00 2026-05-29T10:18:56+00:00

I am rendering PartialViews/Models with the method below in order to template emails that

  • 0

I am rendering PartialViews/Models with the method below in order to template emails that get sent out.

I am using the code below to convert the partial and model into an html string that I can pass to my email sending code.

public class BaseController : Controller
{
    public string RenderPartialViewToString(string viewName, object model)
    {
        if (string.IsNullOrEmpty(viewName))
            throw new ArgumentException("No View Path Provided.");

        ViewData.Model = model;

        using (StringWriter sw = new StringWriter())
        {
            ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
            ViewContext viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
            viewResult.View.Render(viewContext, sw);

            return sw.GetStringBuilder().ToString();
        }
    }
}

Currently, this lives in the BaseController, I would like to move it out into a helper method so that I can move my email model building out/sending out of the controller too.

The problem is I don’t have access to ViewData/TempData/ControllerContext

I can new up a ControllerContext but I don’t know what to do about ViewData/TempData.

This is how I would use what I have in the Controller currently:

//Do Stuff in Controller

var html = RenderPartialViewToString("~/Views/Mail/_ForgotPassword.cshtml", new MailModel { Username = "Skrillex", SomethingElse = "foo" });

//Send the Email
  • 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-05-29T10:18:58+00:00Added an answer on May 29, 2026 at 10:18 am

    I think you are on the right track, but the problem is your eagerness to complete separation, it is rather too eager.

    You are using Razor view engine to render your rich text HTML email. A very noble approach. However, this means that you will be very close to your presentation layer and running this from outside a controller – in my view – does not make a lot of sense.

    I believe you need to make (if not already made):

    • Your email Razor view as strongly typed
    • Let the rendering called in the controller as usual
    • Rendering would be as simple as passing the model to the Render method
    • Take out the building of your email model out to the helper you wish. This would not require any presentation layer logic and as such oblivious to it.

    So the point is, calling of the rendering does not need to go out of the controller, building of the email model should.

    Now if you are doing all of that it means I have not understood your question and requires more explanation.

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

Sidebar

Related Questions

When rendering a django form using the as_p method, you traditionally get something like:
I have controller method in my MVC application that I am calling using the
I'm rendering a drop down box that contains a currently selected value using selected=true
I am rendering models based on milions (up to ten) of triangles using VBOs
I am rendering an OpenGL scene that include some bitmap text. It is my
I have a usercontrol that is rendering a list of items. Each row contains
I am trying to get my TagsController#index action to display only the Questions that
Rendering a partial using AJAX $('#next_videos').replaceWith('<%= render(:partial => 'videos')%>'); is my js the partial
I am having difficulty rendering a partial view using the MVC3 razor engine. When
i did a text rendering program using UIView and UITableViewController. What i did is

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.