It has been a while since my last post on MVC 3. It was quite a hectic few weeks of my life, since there are a lot of task need to be submitted. Finally I manage to steal some time to finish up my tutorial on how to "Create A Simple Blog Application" by using MVC 3. Since my last tutorial, I did manage to write and guide Negative Zero readers until Post Section where the Part 3 tutorial did cover on making new post, list all post and setting ONLY admin can post new articles. For those who misses out three series of "Create A Simple Blog Application", below is the list of past tutorial:
- Part 1 : Cover on creating a new project on MVC 3
- Part 2 : Cover on creating models for our blog application using LightSpeed.
- Part 3 : Cover on creating new post, delete post, edit post and list post(for admin)
- Part 4 : Will be covering on displaying single post and comments. Also, will be covering on new comment controller.
So, FullPost controller will look like this:
Referring code above, on line 26 – we can see that I did bind the data fetch from joined LINQ query on a new model name blogModel. So to use this model, we need to declare the model definition at folder named Models. Create new class inside Models folder, and paste code below:
This model will handle and hold all data from table Post and Comment so that we can use 'Strongly-typed view' when we create the View for FullPost later in this tutorial. Once we have prepare this two files, next we need to create the viewing for FullPost controller in view folder. To create the view, just right click on the Index(int id) method and select option Add View. A popup window will be made available to you to set the view configuration. Select strongly-typed view, and select blogModel as Model Class. On Scaffold option, we need to leave it empty since a lot of code in view need to code by hand. Once we have create the view, paste this code in the view by replacing whatever code that is inside default view template.
And the view which will be the form for new comment is attached in below box:
Once we have finish on Create method view, we need one more piece of code to handle the closure of comment form popup window. To handle this, we create a view named newComment as we named our controller method which process and store the new comment. The one line code is written in the box below:
Once we have all file copied and pasted as requested in this tutorial, we can now try compile our fresh codes to seek if there are any error on the code. By default, there should be no error since I did compile and wrote this code myself. Once compiled, run the application in the browser, and browse to the address of FullPost controller. For those who confuse what is the URL lead to FullPost controller, here it is: http://localhost:<PORT>/FullController/Index/1 – We need to include Index/1 at the URL to provide the post ID of which we would like to view in single page.
For next tutorial, I'll be finishing up my tutorial on Simple Blog Application. So we could say that tutorial on how to create a simple blog application by using MVC 3 is now coming to an end. One more part to go, and then we will have a blog application code using MVC. Until my next tutorial, please leave a comment for any question. Discussion are mostly welcome.