ASP.NET MVC Render View line to send by email

I want to use MVC views to create a body for email, and I came across this ( http://www.brightmix.com/blog/renderpartial-to-string-in-asp-net-mvc/ ), but it doesn’t " It seems to work with strongly typed views (ViewContext is null.) But I was for something that would do a full scan, including the master page.

I thought that if there was a way to just call the view without redirecting and just write to another stream and send email inside the controller, which could do it, but I cannot figure out how to call the view.

Any suggestions would be great!

Thanks in advance.

Gifster

+7
asp.net-mvc asp.net-mvc-views asp.net-mvc-2
source share
2 answers

The question has already been asked (and answered):

Render view as string

This is the bit I'm using:

protected string RenderViewToString<T>(string viewPath, T model, System.Web.Mvc.ControllerContext controllerContext) { using (var writer = new StringWriter()) { var view = new WebFormView(viewPath); var vdd = new ViewDataDictionary<T>(model); var viewCxt = new ViewContext(controllerContext, view, vdd, new TempDataDictionary(), writer); viewCxt.View.Render(viewCxt, writer); return writer.ToString(); } } 

The best place for this method is the class library project, to which your mvc project has a link. Mostly because you can easily use it in all your applications. But also because this is not the application logic (therefore, it does not belong to the controller) and does not belong to the model. Some things are just utilities.

Please note that for this to work, the viewPath parameter must be the PHYSICAL path to the file, complete with the .aspx extension. You cannot use a route because the WebFormView class requires a physical path in its constructor.

This will allow you to fully view and take into account the main page.


HEALTH WARNING FOR HTML E-MAIL:

HTML letters and the devices where you read them are even more complex and restrictive for design than for websites and browsers. What works in one will not work in the other. Thus, with html letters, you really need to keep it simple! Your beautiful page with menus and relative images, and everything else, JUST DOES NOT WORK on all email devices. As an example, the src attribute of images should be absolute and contain a domain:

This will not work:

 <img src="/Images/MyImage.gif" ... /> 

Bit it will be:

  <img src="http://www.mywebsite.com/Images/MyImage.gif" ... /> 

With these caveats, it works fine, and I use it. Just do not try to send them the full trick of your site, because it will not work!

More importantly:

All CSS should be INLINE and just for the basic style: colors, borders, indents. But there is no swimming and positioning. CSS layouts will not work consistently on all devices!

+15
source share

You can use the MvcView NuGet package for this! Simple, just install MvcMailer using the installation package, and you're done! Use the full power of the viewing engine for the template, main page, presentation model and send html letters carefully!

+4
source share

All Articles