Building a Simple Blog Engine with ASP.NET MVC and LINQ - Part 3
page 6 of 8
by Keyvan Nayyeri
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 8194/ 609

Update the Controllers

Very well! After learning about the data model, now it's time to update our implementation of controller classes to apply the data model methods and load data and pass the data to views.

If you can remember from the second part of this series, we had three controller classes with three action methods included in them. Here we updated them to use data model methods.

First controller to update is the HomeController and its Index action method. The updated code is presented in Listing 2.

Listing 2:

using System;
using System.Web;
using System.Web.Mvc;
using KBlog.Models;
using System.Collections.Generic;
 
namespace KBlog.Controllers
{
    public class HomeController : Controller
    {
        // Sample URL: /Default.aspx
        [ControllerAction]
        public void Index()
        {
            KBlogDataContext dataContext = new KBlogDataContext();
            List<Post> posts = dataContext.GetRecentPosts(10);
 
            RenderView("Index", posts);
        }
    }
}

As you see I used the KBlogDataContext class and its GetRecentPosts method to retrieve a list of posts and pass them to RenderView method. RenderView is a method that gets state data and a view name and passes the data to view. The rest will be done in views that we'll discover in future article parts.

The second controller is CategoriesController where we retrieve the data for all the posts in a specified category and the updated code is shown in Listing 3.

Listing 3:

using System;
using System.Web;
using System.Web.Mvc;
using KBlog.Models;
using System.Collections.Generic;
 
namespace KBlog.Controllers
{
    public class CategoriesController : Controller
    {
        // Sample URL: /Category/DotNet
        [ControllerAction]
        public void Category(string name)
        {
            KBlogDataContext dataContext = new KBlogDataContext();
            List<Post> posts = dataContext.GetCategoryPosts(name);
 
            RenderView("Category", posts);
        }
    }
}

The last controller is PostsController that loads data for an individual post and passes it to the appropriate view (Listing 4).

Listing 4:

using System;
using System.Web;
using System.Web.Mvc;
using KBlog.Models;
using System.Collections.Generic;
 
namespace KBlog.Controllers
{
    public class PostsController : Controller
    {
        // Sample URL: /Post/25
        [ControllerAction]
        public void Post(int id)
        {
            KBlogDataContext dataContext = new KBlogDataContext();
            Post post = dataContext.GetPost(id);
 
            RenderView("Post", post);
        }
    }
}

The only thing that you may ask about is the RenderView method and stuff related to views. Hopefully we will cover views in more details in next parts so don't worry about them!


View Entire Article

Article Feedback

Title:  
Name:  
Url: ( Optional )
Comment:  
Please add 4 and 1 and type the answer here:

User Comments

Title: Nevermind   
Name: Nick Kirkes
Date: 4/26/2008 7:07:31 PM
Comment:
Just realized I am missing the DBDataContext.
Title: context.Posts?   
Name: Nick Kirkes
Date: 4/26/2008 7:06:44 PM
Comment:
Thanks for the article.

I'm following your guidance, but I can't build as the "context.Posts" don't exist (same for categories). Am I missing something?
Title: anxiously awaiting...   
Name: Emiel
Date: 4/24/2008 3:06:02 AM
Comment:
Here's another one waiting for part 4...
When, when, WHEN?
Title: And the part 4?   
Name: lxx
Date: 4/17/2008 11:42:43 AM
Comment:
I wait for your next artical.Thanks.
Title: Reply to Mike   
Name: Keyvan Nayyeri
Date: 3/28/2008 8:53:51 AM
Comment:
Mike,

I'll send it to editorial team in the next three days so I expect it to be published in the next couple of weeks.
Title: Expectation?   
Name: Mike Mayers
Date: 3/28/2008 8:45:08 AM
Comment:
Is there any prevision about the release of the 4th part? Can't wait to see it. Very nice.
Title: Very good!!!!!   
Name: Marcelo
Date: 3/27/2008 4:35:06 PM
Comment:
Very good tutorial! Am waiting anxiously for the "View" part!!! Thank you.






Ads Powered by Lake Quincy Media
Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2008 ASPAlliance.com  |  Page Processed at 5/12/2008 4:07:18 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search