2

i have done as Vdex suggested here: https://stackoverflow.com/a/5801502/973485 And used the RenderPartialToString method he found. And it works perfectly like this:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }
    public ActionResult Test()
    {
        string t = ViewToString.RenderPartialToString("Index", null, ControllerContext);
        return Content(t);
    }
}

But if i want to render the Home > Index from another Controller, i get:

Value cannot be null.
Parameter name: controllerContext

Like this:

public class FooController : Controller
{
    public ActionResult Index()
    {
        string t = ViewToString.RenderPartialToString("Index", null, new HomeController().ControllerContext);
        return Content(t);
    }
}

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!

Update: Why i need to do this:

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?

Community
  • 1
  • 1
BjarkeCK
  • 5,694
  • 5
  • 41
  • 59
  • if your just wanted to delivery the home page as the home page of foo/index then you can just return view with a path /Home/Index unless im missing something here – davethecoder Dec 22 '11 at 16:31
  • Year, i am building a CMS system, and i have a Interface for all Widget Controllers. Wich i have a list of. And then the database desides wich Controllers are printent on the page, in the different areas. – BjarkeCK Dec 22 '11 at 16:44
  • can't you just do Html.RenderAction("_CalendarWidget", "Calendar") from your view ? – Bart Calixto Aug 23 '13 at 03:48

1 Answers1

0

Try this:

string t = ViewToString.RenderPartialToString("Index", null, this.ControllerContext);

Anyway, why do you need to convert to a string?

David Fox
  • 10,603
  • 9
  • 50
  • 80
Pedro Fonseca
  • 449
  • 5
  • 10
  • Thank you for your suggestion. this.ControllerContext works! But it's the wrong ControllerContext, it needs to be the ControllerContext from the HomeController. – BjarkeCK Dec 22 '11 at 16:34
  • I am building a Widget System for my CMS, where each Widget has it own Controller with ViewWidget, EditWidget And newWidget ActionResults. – BjarkeCK Dec 22 '11 at 16:36
  • @gdoron Exatyly! ;) But it does not work if i need the ControllerContext from a nother Controller – BjarkeCK Dec 22 '11 at 21:49