Article Management System Using ASP.NET MVC

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.
  1. TBL_ARTICLE (To save the article related information).
  2. 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

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;   
  5. using System.ComponentModel.DataAnnotations.Schema;  
  6. using System.ComponentModel.DataAnnotations;  
  7.   
  8.   
  9. namespace R_ArticleManagementSystem.Models  
  10. {  
  11.     [Table("TBL_Article")]  
  12.     public class Article  
  13.     {  
  14.         [Key]  
  15.         public int ArticleID { get; set; }  
  16.   
  17.         [MaxLength(500)]  
  18.         [Column(TypeName = "varchar")]  
  19.         public string ArticleTitle { get; set; }  
  20.   
  21.         [MaxLength(1000)]  
  22.         [Column(TypeName = "text")]  
  23.         public string ArticleDescription { get; set; }  
  24.   
  25.         [Column(TypeName = "text")]  
  26.         public string ArticleText { get; set; }   
  27.           
  28.         public DateTime ArticlePostDate { get; set; }  
  29.   
  30.         public int TechnologyID { get; set; }   
  31.     }  
  32. }  


Now, again right click on Models folder and add a new class: Technology.cs and execute the code, given below.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.ComponentModel.DataAnnotations;  
  6. using System.ComponentModel.DataAnnotations.Schema;  
  7.   
  8. namespace R_ArticleManagementSystem.Models  
  9. {  
  10.     [Table("TBL_Technology")]  
  11.     public class Technology  
  12.     {  
  13.         [Key]  
  14.         [Required(ErrorMessage = "Technology is required")]  
  15.         public int TechnologyID { get; set; }  
  16.   
  17.         [MaxLength(25)]  
  18.         public string TechnologyName { get; set; }  
  19.     }  
  20. }  


Now, add a context class, which will be responsible for the database activity i.e. ArticleContext.cs and execute the code, given below.
  1. using System.Data.Entity;  
  2.   
  3. namespace R_ArticleManagementSystem.Models  
  4. {  
  5.     public class ArticleContext : DbContext  
  6.     {  
  7.         public ArticleContext() : base("MyConnection")  
  8.         {  
  9.         }  
  10.         public DbSet<Article> Articles { get; set; }  
  11.         public DbSet<Technology> Technologys { get; set; }  
  12.     }  
  13. }  


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.
  1. <connectionStrings>  
  2.     <add name="MyConnection" connectionString="Data Source=.;database = R-ArticleManagement; integrated security=true"   
  3.                                                 providerName="System.Data.SqlClient" />  
  4.     <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-R-ArticleManagementSystem-20160919025329.mdf;Initial Catalog=aspnet-R-ArticleManagementSystem-20160919025329;Integrated Security=True"  
  5.       providerName="System.Data.SqlClient" />  
  6.   </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.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using R_ArticleManagementSystem.Models;  
  7.   
  8. namespace R_ArticleManagementSystem.Controllers  
  9. {  
  10.     public class ArticleController : Controller  
  11.     {  
  12.         ArticleContext db = null;  
  13.          
  14.         public ArticleController()  
  15.         {  
  16.             db = new ArticleContext();  
  17.         }  
  18.   
  19.         // GET: Article  
  20.         public ActionResult Index()  
  21.         {  
  22.             List<SelectListItem> technologyCategories = new List<SelectListItem>();  
  23.             technologyCategories.Add(new SelectListItem { Text = "Select Category", Value = "0", Selected = true });  
  24.             var techCategories = db.Technologys.ToList();  
  25.             foreach (var c in techCategories)  
  26.             {  
  27.                 technologyCategories.Add(new SelectListItem { Text = c.TechnologyName, Value = Convert.ToString(c.TechnologyID) });  
  28.             }  
  29.             ViewBag.TechnologyList = technologyCategories;  
  30.             return View();  
  31.         }  
  32.   
  33.         public JsonResult GetArticleByTechID(int techhId)  
  34.         {  
  35.             List<Article> articles = new List<Article>();  
  36.             articles = db.Articles.Where(x => x.TechnologyID == techhId).Take(10).ToList();  
  37.   
  38.             return Json(articles, JsonRequestBehavior.AllowGet);  
  39.         }  
  40.     }  
  41. }  


Now, right click on Index Action Method -> Add new view.

Index.cshtml
  1. @{  
  2.     ViewBag.Title = "Index";  
  3.     Layout = "~/Views/Shared/_Layout.cshtml";  
  4. }  
  5. <script src="~/Scripts/jquery-1.10.2.min.js"></script>  
  6. <script type="text/javascript">  
  7.     $(document).ready(function () {  
  8.         $("#TechnologyList").change(function () {  
  9.             $.ajax({  
  10.                 type: 'GET',  
  11.                 url: '@Url.Action("GetArticleByTechID")',  
  12.                 datatype: JSON,  
  13.                 data: { 'techhId': $("#TechnologyList").val() },  
  14.                 success: function (data) {  
  15.                     $('#ArticleLisitngTable tbody').empty();  
  16.   
  17.                     $.each(data, function (i, item) {  
  18.                         var rows = "<tr>"  
  19.                 + "<td>" + item.ArticleID + "</td>"  
  20.                 + "<td>" + item.ArticleTitle + "</td>"  
  21.                 + "<td>" + item.ArticleDescription + "</td>"  
  22.                 + "<td>" + item.ArticleText + "</td>"  
  23.                 + "</tr>";  
  24.                         $('#ArticleLisitngTable tbody').append(rows);  
  25.                     });  
  26.                 },  
  27.                 error: function (data) { }  
  28.             });  
  29.         });  
  30.     });  
  31. </script>  
  32. <style type="text/css">  
  33.     .ArtTable {  
  34.         border: solid 1px #DDEEEE;  
  35.         border-collapse: collapse;  
  36.         border-spacing: 0;  
  37.         width: 100%;  
  38.         font: normal 13px Arial, sans-serif;  
  39.     }  
  40.   
  41.         .ArtTable thead th {  
  42.             background-color: #ff6a00;  
  43.             border: solid 1px #DDEEEE;  
  44.             color: #ffffff;  
  45.             padding: 10px;  
  46.             text-align: left;  
  47.         }  
  48.   
  49.         .ArtTable tbody td {  
  50.             border: solid 1px #b0ecee;  
  51.             color: #ff0000;  
  52.             padding: 10px;  
  53.         }  
  54.   
  55.     .ArtTable-rounded {  
  56.         border: none;  
  57.     }  
  58.   
  59.         .ArtTable-rounded thead th {  
  60.             background-color: #ff6a00;  
  61.             border: none;  
  62.             color: #ffffff;  
  63.         }  
  64.   
  65.             .ArtTable-rounded thead th:first-child {  
  66.                 border-radius: 10px 0 0 0;  
  67.             }  
  68.   
  69.             .ArtTable-rounded thead th:last-child {  
  70.                 border-radius: 0 10px 0 0;  
  71.             }  
  72.   
  73.         .ArtTable-rounded tbody td {  
  74.             border: none;  
  75.             border-top: solid 1px #957030;  
  76.             background-color: #ffffff;  
  77.         }  
  78.   
  79.         .ArtTable-rounded tbody tr:last-child td:first-child {  
  80.             border-radius: 0 0 0 10px;  
  81.         }  
  82.   
  83.         .ArtTable-rounded tbody tr:last-child td:last-child {  
  84.             border-radius: 0 0 10px 0;  
  85.         }  
  86. </style>  
  87. <table style="width:100%;padding:40px; font-weight:bold; font-size:12pt;">  
  88.     <tr>  
  89.         <td style="padding-top:40px; background-color:#0094ff; color:white; ">  
  90.             Select Article Category : @Html.DropDownList("TechnologyList")  
  91.         </td>  
  92.     </tr>  
  93. </table>  
  94. <br />  
  95. <table id="ArticleLisitngTable" class="ArtTable ArtTable-rounded">  
  96.     <thead>  
  97.         <tr>  
  98.             <th>Id</th>  
  99.             <th>Title</th>  
  100.             <th>Article Description</th>  
  101.             <th>Article Text</th>  
  102.         </tr>  
  103.     </thead>  
  104.     <tbody></tbody>  
  105. </table>  
Now, run your Application.









In the next part, I will post complete functionality of this MVC project. 

Up Next
    Ebook Download
    View all
    Learn
    View all