3

I have found a few other questions like this, however every single one has not been able to fix this problem I am having. I continuously get the same problem.

I have checked out this and that and multiple others! Every single thing I get the same problem.

I get the following exception:

The view at '~/Views/Email/Ticket.cshtml' must derive from WebViewPage, or WebViewPage<TModel>.

The code I am using is:

public static string RenderViewToString(string controllerName, string viewName, object viewModel)
{
    HttpContextWrapper context = new HttpContextWrapper(HttpContext.Current);
    RouteData routeData = new System.Web.Routing.RouteData();
    routeData.Values.Add("controller", controllerName);
    ControllerContext controllerContext = new ControllerContext(context, routeData, new RenderController());


    RazorViewEngine razorViewEngine = new RazorViewEngine();
    ViewEngineResult razorViewResult = razorViewEngine.FindView(controllerContext, viewName, string.Empty, false);


    using (StringWriter writer = new StringWriter())
    {
        ViewDataDictionary viewData = new ViewDataDictionary(viewModel);
        var viewContext = new ViewContext(controllerContext, razorViewResult.View, viewData, new TempDataDictionary(), writer);
        razorViewResult.View.Render(viewContext, writer);
        writer.Flush();
        return writer.GetStringBuilder().ToString();
    }
}

The RenderController is just:

private class RenderController : ControllerBase
{
    protected override void ExecuteCore()
    {
    }
}

No matter what I have done, or different methods I have tried nothing seems to make this error go away, I have tried making the view inherit the objects but you can't have @model and @inherit in the same thing. Getting quite aggravated over this as I have followed pretty much everyone's instructions on other questions/posts but hasn't been able to help me.

Community
  • 1
  • 1
Lucas
  • 376
  • 3
  • 18

1 Answers1

0

Thanks to @JustinRusso he helped figure this out!

What seemed to happen was, my refernce to MVC 5.0.0.0 in my actual web project was different to my MVC version that I was referencing in my Common library which was at 4.0.0.1.

After I updated the version it worked. I feel stupid now looking back on it but that is what learning is all about!

Thanks to all for the help and suggestions!

I don't think you can mark comments as answers (at least from what I can tell) so just making it a bigger item so future people can find it.

Lucas
  • 376
  • 3
  • 18
  • You're very welcome. Can you vote my answer up and also mark it as the answer? I work for points. LOL – Justin Russo Feb 12 '15 at 17:13
  • I don't see any button to mark a comment as an answer other wise I would have... I did upvote it though. – Lucas Feb 12 '15 at 17:14
  • Hmmm... I don't see the upvote yet. Maybe that's coming later. Can you make sure the "up arrow" is orange? If not, you have to mark it again. Thanks. – Justin Russo Feb 12 '15 at 17:18
  • Yep, it is orange for me. Is that considered marking it as answer? Figured that counted just as a comment up vote. – Lucas Feb 12 '15 at 17:24
  • Well, that means you voted it up. As for marking it as an answer, I believe that exists below the answer itself, as an option. I haven't asked a question in a bit. – Justin Russo Feb 12 '15 at 17:27
  • I see your comment (but not your answer) has gotten an upvote. Upvotes on comments don't generate reputation, if that's what you're wondering :) http://stackoverflow.com/help/privileges/comment – Lars Kristensen Feb 12 '15 at 19:12