RPS
RPS

Reputation: 1401

Passing data from controller to MVC2 User Control

I have a MVC2 user control that I want to dynamically load the menu from the controller.

I will use LINQ to SQL to get the data that I want to pass to the user control.

How can I tell the MVC2 User Control which controller and action to use?

This is in ASP.net MVC2

Upvotes: 0

Views: 843

Answers (2)

jevakallio
jevakallio

Reputation: 35890

You can use the Html.RenderAction helper:

<% Html.RenderAction("ActionName", "ControllerName"); %>

From your controller you should return a PartialViewResult:

public ActionResult ActionName()
{
    var menuItems = DB.GetMenuItems();
    return PartialView("MenuViewName", menuItems);
}

Upvotes: 1

Fabiano
Fabiano

Reputation: 5194

Normally the controller tells which view to use and not vice versa.

But you can use the RenderAction Html helper to call a child action which returns the concrete partial view

Upvotes: 0

Related Questions