0

I'm trying to render a PartialView in my Controller. I need the HTML of the PartialView to be specific. I'm using this code in order to get the HTML. The problem that I have right now is that I don't get the rendered PartialView but the whole View. I need the HTML of the rendered PartialView for an email. The ViewEngineResult doesn't show any possible way of getting the PartialView.

Is there any way of getting the HTML of a PartialView?

This is how I'm using the code from the post

            var partialView = PartialView("Index", viewModel);
            var html = partialView.ToHtml(HttpContext);

I had to change the ViewResult parameter in the ToHtml() Method.

Before

    public static string ToHtml(this ViewResult result, HttpContext httpContext)

After

    public static string ToHtml(this PartialViewResult result, HttpContext httpContext)
Vins
  • 291
  • 1
  • 5
  • 19
  • Please edit your question to show us the code of how you are calling the `ToHtml()` method. – prinkpan Jun 11 '19 at 12:16
  • Possible duplicate of [How to Render Partial View into a String](https://stackoverflow.com/questions/2537741/how-to-render-partial-view-into-a-string) – Rahatur Jun 11 '19 at 12:23
  • Possible duplicate question. You might find the answer here: https://stackoverflow.com/questions/2537741/how-to-render-partial-view-into-a-string – Rahatur Jun 11 '19 at 12:24
  • @Rahatur I tried every single answer in this post and it didn't help me. The reason for this is the version of the MVC framework. I'm currently using asp.net core – Vins Jun 11 '19 at 12:59

2 Answers2

0

return the view you want.

for example purpose, suppose you want this "InvalidConfirmToken" view to return from controller, write:

return View("InvalidConfirmToken");

and remove partial rendering from layout.cshtml:

Html.RenderPartial("_ConfirmEmailAlert"); //remove this line

or put this line at the top of the _ConfirmEmailAlert.cstml:

@{
    Layout = null;
}
0

After a bit of digging I found this. Works perfectly now

Vins
  • 291
  • 1
  • 5
  • 19