0

Possible Duplicate:
Render a view as a string

I want to load a View into a variable so I can send it via Mail.

I'd want it to work like this:

var mail = new SmtpClient();
var mailView = View("registration.cshtml", userModel);
mail.Send(FromAddress, userModel.Email, mailView.ViewBag.Title, mailView.ToString());

everything but the ToString() works. How do I force MVC to compile the view?

1
  • 1
    I ended up using this method from another user on this site. Commented Sep 26, 2012 at 21:51

3 Answers 3

1

Here's a little method for ya to put inside your controller.

    protected string RenderViewToString(string viewName, object model)
    {
        ViewData.Model = model;
        using (var sw = new StringWriter())
        {
            var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
            var viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
            viewResult.View.Render(viewContext, sw);
            viewResult.ViewEngine.ReleaseView(ControllerContext, viewResult.View);
            return sw.GetStringBuilder().ToString();
        }
    }

And then you can use it like this:

string viewString = RenderViewToString("viewName", yourModel);
Sign up to request clarification or add additional context in comments.

7 Comments

What Namespace/Class does this method belong to? I don't have it. Is it MVC4?
@manuFS It's inherited from Controller. I assumed you'd be doing this in a controller.
I am, but it doesn't show up on intellisense for me. :/
Hmm...weird, I was thinking it was part of Controller. Evidently the company I work for had implemented their own. Check my edit in a sec
that looks a lot like the code I used from another SO answer.
|
0

It's unlikely that you're actually going to send a View to a user (that is, a web page that you would render to a browser). What you probably want is a way to render Razor templates to an email. For that, there are several tools you can use. For example, I use FluentEmail, which renders templates quite nicely.

From a software engineering stand point, it's not good practice to have your controllers render an email anyways. This should be encapsulated into business processes. Having your controller render an email violates the Single Responsibility Principal.

1 Comment

FluentEmail has no multi-part support - that is a deal breaker for me.
0

I agree, that sending data from controller is bad idea. For example use MSMQ. Also look at this links:

3 Comments

I have no idea what you are trying to tell me? Where ELSE would I send a mail from if not from a controller? A user registers on my site, he gets two views. One Website, one mail. I really don't get it.
@manuFS The number of threads is limited, so send email with thread that process the request is not good, no guarantee that he will execute quickly (network errors and etc). The idea is: site only create mails and mail service sends them, something like this: Use of MSMQ for Sending Bulk Mails
Ah I see. Well this is no concern here as this is only a registration mail and the site is not expected to have more than 10 users online, ever. If I'd expect a bigger userbase, I'd consider this, thanks.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.