Jul 3, 2011

MVC 3 : Create A Simple Blog Application Pt. 3



For those who missed my last tutorial on MVC on how to create simple blog application can try to review those by links listed below:
On previous tutorial, I've cover much on how we develop our blog models using LightSpeed ORM and directly create the database to SQL.  So in this tutorial I'll assume that we should have our models working.  Previous tutorial did not mention on how we need to set the KeyTable for our database to allow LightSpeed to add new data on the same time add plus one to our Identity Table.


To accomplished this, we need to use the KeyTable SQL and run the query in SQL server management studio.  Below is the SQL Query to insert the KeyTable:
   1: IF EXISTS (SELECT * FROM sysobjects WHERE type = 'U' AND name = 'KeyTable')
   2: BEGIN
   3:   DROP TABLE KeyTable
   4: END;
   5:  
   6: CREATE TABLE KeyTable
   7: (
   8:   NextId INT NOT NULL
   9: )
  10:  
  11: INSERT INTO KeyTable VALUES (1);

After run the query, we should have another table named KeyTable.  Once we have the table, we are done with designing our model on LightSpeed.  Now we can start to work on Post.
On Post section, we should have the idea of what should be inside the Controller.  This is the basic function that we should have on Post Controller that I can think of:
  • List All Post
  • Edit Post
  • Delete Post
  • Create New Post
So today I'll cover on all the function no Post.  First we need to create the PostController in our Controller folder.  To create a new controller, we can simply do the steps below:
  1. Right click on Controller folder on Solution Explorer.
  2. Select New Controller option
  3. Name the Controller as 'PostController'
  4. Click OK.
Now we should have our controller which we named as PostController.  The file should be name as PostController.cs.  Now we open the PostController.cs file and we delete all the content if it does have default template.  We do not need to use any of the default code.  So this is the code that we need for Post Controller:
   1: using System;
   2: using System.Collections.Generic;
   3: using System.Linq;
   4: using System.Web;
   5: using System.Web.Mvc;
   6: using NegativeZero.Models;
   7: using Mindscape.LightSpeed;
   8:  
   9: namespace NegativeZero.Controllers
  10: {
  11:     public class PostController : Controller
  12:     {
  13:         //
  14:         // Declaring UnitOfWork to be used on the controller.  You could do it by declaring
  15:         // it outside of the method, of it could be declared inside method. Both should work.
  16:         LightSpeedContext context = new LightSpeedContext("Development");
  17:         
  18:         //
  19:         // GET: /Post/
  20:         [Authorize]
  21:         public ActionResult Index()
  22:         {
  23:             using (var unitOfWork = context.CreateUnitOfWork())
  24:             {
  25:                 var Post = unitOfWork.Find<Post>();
  26:                 return View(Post.ToList());
  27:             }
  28:         }
  29:  
  30:         public ActionResult Edit(int id)
  31:         {
  32:             using (var unitOfWork = context.CreateUnitOfWork())
  33:             {
  34:                 var PostToEdit = unitOfWork.Find<Post>().Where(c => c.Id.Equals(id)).First();
  35:                 if (!PostToEdit.Equals(null))
  36:                 {
  37:                     return View(PostToEdit);
  38:                 }
  39:                 else
  40:                 {
  41:                     return View();
  42:                 }
  43:             }
  44:         }
  45:  
  46:         [HttpPost]
  47:         public ActionResult Edit(string Id, string Title, string Author, string Posts, string Tag, string Categories)
  48:         {
  49:             using (var unitOfWork = context.CreateUnitOfWork())
  50:             {
  51:                 var PostToEdit = unitOfWork.Find<Post>().Where(c => c.Id.Equals(Id)).FirstOrDefault();
  52:                 PostToEdit.Title = Title;
  53:                 PostToEdit.Author = Author;
  54:                 PostToEdit.Posts = Posts;
  55:                 PostToEdit.Tag = Tag;
  56:                 PostToEdit.Categories = Categories;
  57:                 unitOfWork.SaveChanges();
  58:             }
  59:             return Redirect("Index");
  60:         }
  61:  
  62:         public ActionResult Create()
  63:         {
  64:             return View();
  65:         }
  66:  
  67:         [HttpPost]
  68:         public ActionResult Create(string Title, string Author, string Posts, string Tag, string Categories)
  69:         {
  70:             var data = new Post();
  71:             if (Title.Length > 0 && Author.Length > 0 && Posts.Length > 0)
  72:             {
  73:                 data.Title = Title;
  74:                 data.Author = Author;
  75:                 data.Posts = Posts;
  76:                 if (Tag.Length > 0)
  77:                     data.Tag = Tag;
  78:                 else
  79:                     data.Tag = null;
  80:                 if (Categories.Length > 0)
  81:                     data.Categories = Categories;
  82:                 else
  83:                     data.Categories = "Uncategorized";
  84:                 // Store data into SQL db
  85:                 using (var unitOfWork = context.CreateUnitOfWork())
  86:                 {
  87:                     unitOfWork.Add(data);
  88:                     unitOfWork.SaveChanges();
  89:                 }
  90:                 
  91:                 return Redirect("Index");
  92:             }
  93:             else
  94:             {
  95:                 return View();
  96:             }
  97:         }
  98:  
  99:         public ActionResult Delete(int id)
 100:         {
 101:             using (var unitOfWork = context.CreateUnitOfWork())
 102:             {
 103:                 var postToDelete = unitOfWork.Find<Post>().Where(c => c.Id.Equals(id)).Single();
 104:                 if (postToDelete.IsValid)
 105:                 {
 106:                     unitOfWork.Remove(postToDelete);
 107:                     unitOfWork.SaveChanges();
 108:                 }                    
 109:             }
 110:             return RedirectToAction("Index");
 111:         }
 112:     }
 113: }
Once we have this code in our controller, we can now create our view.  To create our view, MVC come with simple method. Below is the step to create our Viewing based on the Controller that we have built just now:
  1. Right click on the method name.  For example, method Index().
  2. Select Add View.
  3. Select strong view type option and select List.
  4. On model option, we choose Post (NegativeZero.Models)
This step will automatically create view for you based on scaffold template that we select. For Index() method, we will use the List Scaffold as we would like to list all posted post by default.  While on Edit() we will select Edit Scaffold to create a view that will allow author to edit the selected post.  For Create() method, we use Create scaffold so that we could have plain form to make our new Post.  Mean while, for Delete() method, I do not use any viewing since my controller will return the view to the Index action once the delete process success.


image

So, now we already half way done on finishing our project to create a simple blog application. On my next tutorial, I'll focus on comments.

Any question and suggestion are mostly welcome.  Please like this post and drop comment(s) if you this this is useful.

Blogger Labels: Create,Simple,Blog,Application,tutorial,links,Part,database,Previous,KeyTable,data,Table,server,management,studio,Query,SELECT,FROM,WHERE,BEGIN,DROP,NextId,NULL,INSERT,INTO,VALUES,Once,Post,Controller,List,Edit,Delete,PostController,folder,steps,Solution,Explorer,option,Name,Click,template,code,System,Generic,Linq,NegativeZero,Mindscape,UnitOfWork,method,Both,LightSpeedContext,context,Development,Authorize,ActionResult,Index,CreateUnitOfWork,Find,View,ToList,PostToEdit,Equals,HttpPost,Title,Author,FirstOrDefault,SaveChanges,Redirect,Length,Single,IsValid,Remove,RedirectToAction,example,scaffold,Mean,action,comments,suggestion,Collections,Controllers,Categories,postToDelete


Thank you for your unbelievable support on Negative Zero - Permission to read and write blog for nearly 4 years. Don't forget to like Negative Zero on Facebook.
Blogirific.com Blog Directory





Post(s) you might like to read :

2 comments:

  1. thanks sir... i like it your this work very much.. i am following your tutotial... your next turial will publish???

    I need.... sir.. plese publish it as soon as posbile

    ReplyDelete