Get HTML from .Net MVC View without actually displaying the view in the browser?

I have an ActionResult in my controller from which I want to send an HTML letter, the body of this letter is generated by the view. Instead of having 2 actionresults methods in my controller, can I just get the viewing result when I passed my model and not send it to the browser?

+3
source share
2 answers

MvcMailer is a brilliant little project that supports the creation of emails using MVC views. It is available as a NuGet package.

To display the view in a line instead of an answer, use this code ( relativePathpoints to your view file):

        var content = string.Empty;
        var view = ViewEngines.Engines.FindView(ControllerContext, relativePath, null);
        using (var writer = new StringWriter())
        {
            var context = new ViewContext(ControllerContext, view.View, ViewData, TempData, writer);
            view.View.Render(context, writer);
            writer.Flush();
            content = writer.ToString();
        }
+8

NuGet.

Postal MVC.

Postal mvcConf 2

, , .

+4

Source: https://habr.com/ru/post/1796035/


All Articles