I just started with ASP.NET MVC and doing a whole new project with it.
Here is my first question, hope it's not too silly, see if someone can help out on this:
I have a main index page where I need to show my "Featured cars" listing and a Search box to search through the inventory. It's a car dealer web site. All this will be inside a ContentPlaceHolder.
Now my question, is how to accomplish this, since I probably will be dealing with two different controllers in the same view ???
Is Html.RenderAction(...) the MVC version of Html.RenderUserControl(...) ?
It's more like a replacement for ComponentController. Html.RenderUserControl() is still around. The main difference is that Html.RenderAction() calls into a controller so that you can perform whatever logic you need to set up the data for your inline view,
and Html.RenderUserControl() takes some preexisting data (the current ViewData, for example) and hands it directly to a new view that is rendered inline.
dimi3
Is Html.RenderAction(...) in Preview 4 only?
Yes. It's part of Microsoft.Web.Mvc.dll in Preview 4.
Marked as answer by tgmdbm on Aug 22, 2008 10:53 AM
brucesinner
Member
1 Points
48 Posts
2 controllers 1 View
Aug 22, 2008 12:26 AM|LINK
Hi folks,
I just started with ASP.NET MVC and doing a whole new project with it.
Here is my first question, hope it's not too silly, see if someone can help out on this:
I have a main index page where I need to show my "Featured cars" listing and a Search box to search through the inventory. It's a car dealer web site. All this will be inside a ContentPlaceHolder.
Now my question, is how to accomplish this, since I probably will be dealing with two different controllers in the same view ???
thanks for any help
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)
dimi3
Member
318 Points
166 Posts
Re: 2 controllers 1 View
Aug 22, 2008 07:02 AM|LINK
@tgmdbm,
Very interesting. Is Html.RenderAction(...) the MVC version of Html.RenderUserControl(...) ?
Is Html.RenderAction(...) in Preview 4 only? (I use Preview 3).
Intellisense doesn't let Html.RenderAction(...) appear.
levib
Star
7702 Points
1099 Posts
Microsoft
Re: 2 controllers 1 View
Aug 22, 2008 07:44 AM|LINK
It's more like a replacement for ComponentController. Html.RenderUserControl() is still around. The main difference is that Html.RenderAction() calls into a controller so that you can perform whatever logic you need to set up the data for your inline view, and Html.RenderUserControl() takes some preexisting data (the current ViewData, for example) and hands it directly to a new view that is rendered inline.
Yes. It's part of Microsoft.Web.Mvc.dll in Preview 4.
brucesinner
Member
1 Points
48 Posts
Re: 2 controllers 1 View
Aug 22, 2008 07:48 AM|LINK
Well, I'm using Preview 3, what should I do then ???
levib
Star
7702 Points
1099 Posts
Microsoft
Re: 2 controllers 1 View
Aug 22, 2008 07:53 AM|LINK
You should consider upgrading to Preview 4. It contains not only extra functionality such as that listed above, but numerous bug fixes as well.
dimi3
Member
318 Points
166 Posts
Re: 2 controllers 1 View
Aug 22, 2008 08:02 AM|LINK
Thank you levib for your precise answers.
Will I need to change some code, web.config files, global.asax, ... in order to migrate from Preview 3 to Preview 4.
If yes, how?
brucesinner
Member
1 Points
48 Posts
Re: 2 controllers 1 View
Aug 22, 2008 09:23 AM|LINK
I checked on ASP.NET MVC website and didn't find the Preview 4 for download...is there any other source I can download the new version ???
thnxs
dimi3
Member
318 Points
166 Posts
Re: 2 controllers 1 View
Aug 22, 2008 09:34 AM|LINK
Preview 4 is on Codeplex. Here is the link for anything you need about Preview 4:
http://www.codeplex.com/aspnet/Release/ProjectReleases.aspx?ReleaseId=15389
brucesinner
Member
1 Points
48 Posts
Re: 2 controllers 1 View
Aug 22, 2008 06:26 PM|LINK
I found it on Codeplex, nevermind.