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. namespace R_ArticleManagementSystem.Models
  8. {
  9. [Table("TBL_Article")]
  10. public class Article
  11. {
  12. [Key]
  13. public int ArticleID { get; set; }
  14. [MaxLength(500)]
  15. [Column(TypeName = "varchar")]
  16. public string ArticleTitle { get; set; }
  17. [MaxLength(1000)]
  18. [Column(TypeName = "text")]
  19. public string ArticleDescription { get; set; }
  20. [Column(TypeName = "text")]
  21. public string ArticleText { get; set; }
  22. public DateTime ArticlePostDate { get; set; }
  23. public int TechnologyID { get; set; }
  24. }
  25. }


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. namespace R_ArticleManagementSystem.Models
  8. {
  9. [Table("TBL_Technology")]
  10. public class Technology
  11. {
  12. [Key]
  13. [Required(ErrorMessage = "Technology is required")]
  14. public int TechnologyID { get; set; }
  15. [MaxLength(25)]
  16. public string TechnologyName { get; set; }
  17. }
  18. }


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. namespace R_ArticleManagementSystem.Models
  3. {
  4. public class ArticleContext : DbContext
  5. {
  6. public ArticleContext() : base("MyConnection")
  7. {
  8. }
  9. public DbSet<Article> Articles { get; set; }
  10. public DbSet<Technology> Technologys { get; set; }
  11. }
  12. }


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. namespace R_ArticleManagementSystem.Controllers
  8. {
  9. public class ArticleController : Controller
  10. {
  11. ArticleContext db = null;
  12. public ArticleController()
  13. {
  14. db = new ArticleContext();
  15. }
  16. // GET: Article
  17. public ActionResult Index()
  18. {
  19. List<SelectListItem> technologyCategories = new List<SelectListItem>();
  20. technologyCategories.Add(new SelectListItem { Text = "Select Category", Value = "0", Selected = true });
  21. var techCategories = db.Technologys.ToList();
  22. foreach (var c in techCategories)
  23. {
  24. technologyCategories.Add(new SelectListItem { Text = c.TechnologyName, Value = Convert.ToString(c.TechnologyID) });
  25. }
  26. ViewBag.TechnologyList = technologyCategories;
  27. return View();
  28. }
  29. public JsonResult GetArticleByTechID(int techhId)
  30. {
  31. List<Article> articles = new List<Article>();
  32. articles = db.Articles.Where(x => x.TechnologyID == techhId).Take(10).ToList();
  33. return Json(articles, JsonRequestBehavior.AllowGet);
  34. }
  35. }
  36. }


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. $.each(data, function (i, item) {
  17. var rows = "<tr>"
  18. + "<td>" + item.ArticleID + "</td>"
  19. + "<td>" + item.ArticleTitle + "</td>"
  20. + "<td>" + item.ArticleDescription + "</td>"
  21. + "<td>" + item.ArticleText + "</td>"
  22. + "</tr>";
  23. $('#ArticleLisitngTable tbody').append(rows);
  24. });
  25. },
  26. error: function (data) { }
  27. });
  28. });
  29. });
  30. </script>
  31. <style type="text/css">
  32. .ArtTable {
  33. border: solid 1px #DDEEEE;
  34. border-collapse: collapse;
  35. border-spacing: 0;
  36. width: 100%;
  37. font: normal 13px Arial, sans-serif;
  38. }
  39. .ArtTable thead th {
  40. background-color: #ff6a00;
  41. border: solid 1px #DDEEEE;
  42. color: #ffffff;
  43. padding: 10px;
  44. text-align: left;
  45. }
  46. .ArtTable tbody td {
  47. border: solid 1px #b0ecee;
  48. color: #ff0000;
  49. padding: 10px;
  50. }
  51. .ArtTable-rounded {
  52. border: none;
  53. }
  54. .ArtTable-rounded thead th {
  55. background-color: #ff6a00;
  56. border: none;
  57. color: #ffffff;
  58. }
  59. .ArtTable-rounded thead th:first-child {
  60. border-radius: 10px 0 0 0;
  61. }
  62. .ArtTable-rounded thead th:last-child {
  63. border-radius: 0 10px 0 0;
  64. }
  65. .ArtTable-rounded tbody td {
  66. border: none;
  67. border-top: solid 1px #957030;
  68. background-color: #ffffff;
  69. }
  70. .ArtTable-rounded tbody tr:last-child td:first-child {
  71. border-radius: 0 0 0 10px;
  72. }
  73. .ArtTable-rounded tbody tr:last-child td:last-child {
  74. border-radius: 0 0 10px 0;
  75. }
  76. </style>
  77. <table style="width:100%;padding:40px; font-weight:bold; font-size:12pt;">
  78. <tr>
  79. <td style="padding-top:40px; background-color:#0094ff; color:white; ">
  80. Select Article Category : @Html.DropDownList("TechnologyList")
  81. </td>
  82. </tr>
  83. </table>
  84. <br />
  85. <table id="ArticleLisitngTable" class="ArtTable ArtTable-rounded">
  86. <thead>
  87. <tr>
  88. <th>Id</th>
  89. <th>Title</th>
  90. <th>Article Description</th>
  91. <th>Article Text</th>
  92. </tr>
  93. </thead>
  94. <tbody></tbody>
  95. </table>
Now, run your Application.









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