Skip to content

Commit 28f1666

Browse files
author
breno.dev
committed
First Model-Controller-View - Department
1 parent ab4df44 commit 28f1666

File tree

7 files changed

+80
-2
lines changed

7 files changed

+80
-2
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Mvc;
6+
using SalesWebMvc.Models;
7+
8+
namespace SalesWebMvc.Controllers
9+
{
10+
public class DepartmentsController : Controller
11+
{
12+
public IActionResult Index()
13+
{
14+
15+
List<Department> list = new List<Department>();
16+
list.Add(new Department { Id = 1, Name = "Electronics" });
17+
list.Add(new Department { Id = 2, Name = "Fashion" });
18+
19+
return View(list);
20+
}
21+
}
22+
}

SalesWebMvc/Controllers/HomeController.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Threading.Tasks;
66
using Microsoft.AspNetCore.Mvc;
77
using SalesWebMvc.Models;
8+
using SalesWebMvc.Models.ViewModels;
89

910
namespace SalesWebMvc.Controllers
1011
{

SalesWebMvc/Models/Department.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
6+
namespace SalesWebMvc.Models
7+
{
8+
public class Department
9+
{
10+
public int Id { get; set; }
11+
public string Name { get; set; }
12+
}
13+
}

SalesWebMvc/Models/ErrorViewModel.cs renamed to SalesWebMvc/Models/ViewModels/ErrorViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System;
22

3-
namespace SalesWebMvc.Models
3+
namespace SalesWebMvc.Models.ViewModels
44
{
55
public class ErrorViewModel
66
{

SalesWebMvc/SalesWebMvc.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<ItemGroup>
88
<PackageReference Include="Microsoft.AspNetCore.App" />
99
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.1.2" PrivateAssets="All" />
10+
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.1.1" />
1011
</ItemGroup>
1112

1213
</Project>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
@model IEnumerable<SalesWebMvc.Models.Department>
2+
3+
@{
4+
ViewData["Title"] = "Departments";
5+
}
6+
7+
<h2>Departments</h2>
8+
9+
<p>
10+
<a asp-action="Create">Create New</a>
11+
</p>
12+
<table class="table">
13+
<thead>
14+
<tr>
15+
<th>
16+
@Html.DisplayNameFor(model => model.Id)
17+
</th>
18+
<th>
19+
@Html.DisplayNameFor(model => model.Name)
20+
</th>
21+
<th></th>
22+
</tr>
23+
</thead>
24+
<tbody>
25+
@foreach (var item in Model) {
26+
<tr>
27+
<td>
28+
@Html.DisplayFor(modelItem => item.Id)
29+
</td>
30+
<td>
31+
@Html.DisplayFor(modelItem => item.Name)
32+
</td>
33+
<td>
34+
@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
35+
@Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
36+
@Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
37+
</td>
38+
</tr>
39+
}
40+
</tbody>
41+
</table>

SalesWebMvc/Views/Shared/Error.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@model ErrorViewModel
1+
@model SalesWebMvc.Models.ViewModels.ErrorViewModel
22
@{
33
ViewData["Title"] = "Error";
44
}

0 commit comments

Comments
 (0)