- Create a Model
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
namespace TaskManagement.Models
{
public class Project
{
public List<Project> ProjectModel = new List<Project>(); // this ProjectModel will hold all the properties in Project Model
[Key]
public int Id { get; set; }
[DisplayName("Project Name")] //data annotation with the tab helper
public string name { get; set; }
[DisplayName("Person Incharge")]
public string pic { get; set; }
[DisplayName("Due Date")]
public string duedate { get; set; }
[DisplayName("Project Detail")]
public string detail { get; set; }
}
}
- Create a Controller
namespace TaskManagement.Controllers
{
public class ProjectController : Controller
{
public IActionResult Index()
{
Project list = new Project();
string sql = "SELECT * FROM td_tms";
Database db = new Database(sql);
db.Open();
if (db.data.HasRows)
{
while (db.data.Read())
{
list.ProjectModel.Add(new Project
{
Id = Int32.Parse(db.data[0].ToString()),
name = db.data[1].ToString(),
pic = db.data[2].ToString()
});
}
}
db.Close();
List<Project> model = list.ProjectModel.ToList();
return View(model);
}
}
}
- Create the View
@model IEnumerable<TaskManagement.Models.Project>
@{
ViewData["Title"] = "Index";
}
<h1>Index</h1>
<div class="col-6 text-right">
<a asp-controller="Project" asp-action="Create" class="btn btn-primary" >Another Create</a>
@Html.ActionLink("Create New", "Create")
</div>
<table class="table" id="table_data">
<tr>
<th>
Project Name
</th>
<th>
Person in Charge
</th>
<th></th>
</tr>
@if (Model.Count() > 0)
{
@foreach (var item in Model)
{
if (item.pic != null)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.name)
@item.name
</td>
<td>
@item.pic
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
@Html.ActionLink("Details", "Details", new { id = item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id = item.Id })
</td>
</tr>
}
}
}