Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Contributor
4392 Points
883 Posts
ASPInsiders
MVP
Aug 22, 2008 02:42 AM|LINK
You should create a controller called CarsController, then add a public method Featured which returns a ViewResult passing in a list of featured cars.
public class CarsController : Controller { private ICarService CarService; public CarsController(ICarService carService) { CarService = carService; } public ActionResult Featured() { var featuredCars = CarService.GetFeaturedCars; return View(featuredCars); } }
Then you can create your view user control, call it Featured.ascx and put it in ~/Views/Cars/. It should be of type ViewUserControl<IEnumerable<Car>>.
Then in your main index view do the following <%= Html.RenderAction( "Featured", "Cars" ) %>
This will create your CarsController, call the Featured method, and render your Featured.ascx in place.
(You can ignore the bit about the CarService if you like, that's just to show a good testable design practice)
tgmdbm
Contributor
4392 Points
883 Posts
ASPInsiders
MVP
Re: 2 controllers 1 View
Aug 22, 2008 02:42 AM|LINK
You should create a controller called CarsController, then add a public method Featured which returns a ViewResult passing in a list of featured cars.
public class CarsController : Controller { private ICarService CarService; public CarsController(ICarService carService) { CarService = carService; } public ActionResult Featured() { var featuredCars = CarService.GetFeaturedCars; return View(featuredCars); } }Then you can create your view user control, call it Featured.ascx and put it in ~/Views/Cars/. It should be of type ViewUserControl<IEnumerable<Car>>.
Then in your main index view do the following <%= Html.RenderAction( "Featured", "Cars" ) %>
This will create your CarsController, call the Featured method, and render your Featured.ascx in place.
(You can ignore the bit about the CarService if you like, that's just to show a good testable design practice)