0

I want to send an email in MVC 5 application. I have the email body store in a file called _EmailBody.cshtml

I am following the similar approach described here Render a view as a string

and I have my setup like this

public static string RenderRazorViewToString(this Controller controller, string viewName, object model)
    {
        controller.ViewData.Model = model;
        using (var sw = new StringWriter())
        {
            var viewResult = ViewEngines.Engines.FindPartialView(controller.ControllerContext, viewName);

            // viewName is "~/Views/Applications/Emails/_EmailBody.cshtml" 
            // blah blah blah

            return "....";
        }
    }

The error happens in this line

var viewResult = ViewEngines.Engines.FindPartialView(controller.ControllerContext, viewName);

and the error is

object reference not set to an instance of an object

Do you have any idea what is causing the problem?

Thank you

Community
  • 1
  • 1
CB4
  • 690
  • 3
  • 13
  • 25
  • @CodeCaster, if you know the answer then show it. – CB4 Nov 09 '16 at 20:57
  • Something is `null`. See the duplicate for debugging hints. Once you find out _what_ is `null`, you can [edit] your question to include your research, as explained in [ask], – CodeCaster Nov 09 '16 at 20:59
  • why the debugger doesn't tell you which object is null. – CB4 Nov 10 '16 at 17:12

0 Answers0