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?

Community
  • 1
  • 1
Manuel Schweigert
  • 4,884
  • 4
  • 20
  • 32
  • 1
    I ended up using [this](http://stackoverflow.com/questions/4344533/asp-net-mvc-razor-how-to-render-a-razor-partial-views-html-inside-the-controll/4344602#4344602) method from another user on this site. – Manuel Schweigert Sep 26 '12 at 21:51

3 Answers3

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);
Phillip Schmidt
  • 8,805
  • 3
  • 43
  • 67
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.

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
0

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

Community
  • 1
  • 1
webdeveloper
  • 17,174
  • 3
  • 48
  • 47
  • 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. – Manuel Schweigert Sep 26 '12 at 22:19
  • @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](http://www.codeproject.com/Articles/165576/Use-of-MSMQ-for-Sending-Bulk-Mails) – webdeveloper Sep 27 '12 at 07:09
  • 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. – Manuel Schweigert Sep 27 '12 at 13:28