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:
- Part 1 – Create Simple Blog Application
- Part 2 – Create Simple Blog Application
- Part 3 - You are reading the tutorial at this moment
- Part 4 - Create Simple Blog Application
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
- Right click on Controller folder on Solution Explorer.
- Select New Controller option
- Name the Controller as 'PostController'
- Click OK.
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: }
- Right click on the method name. For example, method Index().
- Select Add View.
- Select strong view type option and select List.
- On model option, we choose Post (NegativeZero.Models)
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.
thanks sir... i like it your this work very much.. i am following your tutotial... your next turial will publish???
ReplyDeleteI need.... sir.. plese publish it as soon as posbile
Has been published.
ReplyDelete