Note that there are some explanatory texts on larger screens.

plurals
  1. POView to String from another controller
    text
    copied!<p>i have done as Vdex suggested here: <a href="https://stackoverflow.com/a/5801502/973485">https://stackoverflow.com/a/5801502/973485</a> And used the <code>RenderPartialToString</code> method he found. And it works perfectly like this:</p> <pre><code>public class HomeController : Controller { public ActionResult Index() { return View(); } public ActionResult Test() { string t = ViewToString.RenderPartialToString("Index", null, ControllerContext); return Content(t); } } </code></pre> <p>But if i want to render the Home > Index from another Controller, i get:</p> <pre><code>Value cannot be null. Parameter name: controllerContext </code></pre> <p>Like this:</p> <pre><code>public class FooController : Controller { public ActionResult Index() { string t = ViewToString.RenderPartialToString("Index", null, new HomeController().ControllerContext); return Content(t); } } </code></pre> <p>Is there any way to pass a View from another Controller to a string? I have tried many different methods, and it all of them fails at the ControllerContext. Many thanks!</p> <p><strong>Update: Why i need to do this:</strong></p> <p>Imagine i have a website full of widgets, the amount of widgets on each page is dynamic, so i cannot hardcode them in my cshtml file. But in that file there are different areas defined where the widgets gets printet out. To print out these widget i have a list of IWidgetController wich contains alle the different Widgets available, and the interface sais that they need to containe a ActionResult for edit, new and view. example of widgets: CalenderController, NewsController, GalleryController and so on... So in those areas i need to print out the content of each of those Controllers. Now i could also load the URLHTML but i figured doing it from the inside would be faster... right?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload