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):

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

Popular Posts

Resolving the "n timer(s) still in the queue" Error In Angular Unit Tests

How to Get Norton Security Suite Firewall to Allow Remote Desktop Connections in Windows

How to Determine if a Column Exists in a DataReader

Fixing the "Please add a @Pipe/@Directive/@Component annotation" Error In An Angular App After Upgrading to webpack 4

Silent Renew and the "login_required" Error When Using oidc-client