Using a View From a Different Controller in ASP.NET MVC
I'm currently working on a pet project in ASP.NET MVC: an MVC blog engine. I don't plan on using it to host my blog, but rather as an exercise in ASP.NET MVC.
In my HomeController class, I want to retrieve the ten most recent blog posts and render them using the Index view for the BlogPost controller. This way, when a user navigates to the default page of the blog, they'll see a list of the most recent entires. But how could I use a BlogPost view (that isn't part of the Shared views folder) from within the HomeController class?
To achieve this, I did the following in the Index() method of my HomeController class -- notice the line in bold (this is in MVC 3):
In my HomeController class, I want to retrieve the ten most recent blog posts and render them using the Index view for the BlogPost controller. This way, when a user navigates to the default page of the blog, they'll see a list of the most recent entires. But how could I use a BlogPost view (that isn't part of the Shared views folder) from within the HomeController class?
To achieve this, I did the following in the Index() method of my HomeController class -- notice the line in bold (this is in MVC 3):
public ActionResult Index() { ViewBag.Message = "My MVC-Powered Blog!"; BlogRepository repo = new BlogRepository(); BlogRepository.BlogPostCriteria criteria = new BlogRepository.BlogPostCriteria { NumberToRetrieve = 10 }; var blogPosts = repo.GetBlogPosts(criteria); return View("../BlogPost/Index", blogPosts); }
Comments
Post a Comment