views:

27

answers:

1

Hi I have no clue how to use two different controllers at the same time and pass data to one view? I mean I have my BlogController and StatisticsController. Statistics among other things is showing how many people is online and I wanted to this data would be on main page of my blog. I can see two ways that it could work: -create some ContainerController and make objects of BlogController and StatisticsController and there manipulating both methods -create object of StatisticsController in BlogController but I'm not sure about theese solutions. Any advise?

+1  A: 

Your question is extremely unclear. From what I understand you have a controller action returning a view that needs to incorporate some information on a part of the screen. This information is accessible through another controller action returning a partial view. If this is the case then the Html.RenderAction helper might be exactly what you are looking for.

public class BlogController : Controller
{
    public ActionResult Index()
    {
        return View();
    }
}

public class StatisticsController : Controller
{
    [ChildActionOnly]
    public ActionResult Index()
    {
        return PartialView();
    }
}

And in the Index view of the blog controller include the statistics:

<%= Html.RenderAction("Index", "Statistics") %>

The same could be achieved including directly a partial view without the need of passing through a controller:

<%= Html.RenderPartial("~/views/statistics/index.ascx") %>

but the advantage of the RenderAction is that if the statistics requires some specific repository it would be better to pass through a controller.

Darin Dimitrov