Here, I am going to present a project in MVC ASP.NET. This is the first part of my project.
About This Project:
Here, I am going to make a project on Article Management System. Here, the user can post an article, view all articles, add new technology etc. In this first part, I am going to show how we can show all articles in a list. Here, I am going to use Code First approach.
Now, I am going to explain this step by step:
Open Visual Studio 2015 -> Add new project.
Initially in this project, I am going to use two tables.
- TBL_ARTICLE (To save the article related information).
- TBL_TECHNOLOGY (To save the technology information).
As I told you, I am going to use Code First approach. Hence, there is no need to create a database. It will create automatically, when you will run your Application first.
For each table you need in your project you have to create class in your Application: Right click on Models folder and add new class: Article.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.ComponentModel.DataAnnotations.Schema;
- using System.ComponentModel.DataAnnotations;
-
-
- namespace R_ArticleManagementSystem.Models
- {
- [Table("TBL_Article")]
- public class Article
- {
- [Key]
- public int ArticleID { get; set; }
-
- [MaxLength(500)]
- [Column(TypeName = "varchar")]
- public string ArticleTitle { get; set; }
-
- [MaxLength(1000)]
- [Column(TypeName = "text")]
- public string ArticleDescription { get; set; }
-
- [Column(TypeName = "text")]
- public string ArticleText { get; set; }
-
- public DateTime ArticlePostDate { get; set; }
-
- public int TechnologyID { get; set; }
- }
- }
Now, again right click on Models folder and add a new class: Technology.cs and execute the code, given below.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.ComponentModel.DataAnnotations;
- using System.ComponentModel.DataAnnotations.Schema;
-
- namespace R_ArticleManagementSystem.Models
- {
- [Table("TBL_Technology")]
- public class Technology
- {
- [Key]
- [Required(ErrorMessage = "Technology is required")]
- public int TechnologyID { get; set; }
-
- [MaxLength(25)]
- public string TechnologyName { get; set; }
- }
- }
Now, add a context class, which will be responsible for the database activity i.e. ArticleContext.cs and execute the code, given below.
- using System.Data.Entity;
-
- namespace R_ArticleManagementSystem.Models
- {
- public class ArticleContext : DbContext
- {
- public ArticleContext() : base("MyConnection")
- {
- }
- public DbSet<Article> Articles { get; set; }
- public DbSet<Technology> Technologys { get; set; }
- }
- }
Here, you will notice I am using :base("MyConnection") this MyConnection is my connection string, which should exist in your web.config file, as given below.
- <connectionStrings>
- <add name="MyConnection" connectionString="Data Source=.;database = R-ArticleManagement; integrated security=true"
- providerName="System.Data.SqlClient" />
- <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-R-ArticleManagementSystem-20160919025329.mdf;Initial Catalog=aspnet-R-ArticleManagementSystem-20160919025329;Integrated Security=True"
- providerName="System.Data.SqlClient" />
- </connectionStrings>
Now, run your Application or you can run it after adding your desired controller and views, if you don’t have controller or views to run your Application.
It will create a database and tables in your database Server.
After running your Application, it checks your SQL Server data base.
Now, insert some manual entry in your database, as I am going to show the listing of articles only in this part of the project. In the next part, I will show how we can insert the data in this project.
Now, its time to add a new controller -> Right click on Controller folder -> Add new controller.
Add ArticleController.cs and execute the code, given below.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using R_ArticleManagementSystem.Models;
-
- namespace R_ArticleManagementSystem.Controllers
- {
- public class ArticleController : Controller
- {
- ArticleContext db = null;
-
- public ArticleController()
- {
- db = new ArticleContext();
- }
-
-
- public ActionResult Index()
- {
- List<SelectListItem> technologyCategories = new List<SelectListItem>();
- technologyCategories.Add(new SelectListItem { Text = "Select Category", Value = "0", Selected = true });
- var techCategories = db.Technologys.ToList();
- foreach (var c in techCategories)
- {
- technologyCategories.Add(new SelectListItem { Text = c.TechnologyName, Value = Convert.ToString(c.TechnologyID) });
- }
- ViewBag.TechnologyList = technologyCategories;
- return View();
- }
-
- public JsonResult GetArticleByTechID(int techhId)
- {
- List<Article> articles = new List<Article>();
- articles = db.Articles.Where(x => x.TechnologyID == techhId).Take(10).ToList();
-
- return Json(articles, JsonRequestBehavior.AllowGet);
- }
- }
- }
Now, right click on Index Action Method -> Add new view.
Index.cshtml
Now, run your Application.
In the next part, I will post complete functionality of this MVC project.