Skip to content

Commit 00b46af

Browse files
author
C. Brown
committed
1 parent fa5d4fd commit 00b46af

14 files changed

+267
-8
lines changed

CheeseMVC.sln

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,7 @@ Global
3535
GlobalSection(NestedProjects) = preSolution
3636
{6AFD9555-CD44-4530-AEFF-15264D392DF0} = {E4540E26-4149-4E03-AA4B-FDC5D690A52F}
3737
EndGlobalSection
38+
GlobalSection(ExtensibilityGlobals) = postSolution
39+
SolutionGuid = {E758D2EE-C9E2-4AEE-9E38-5EEB0A3C9ED7}
40+
EndGlobalSection
3841
EndGlobal
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Mvc;
6+
using CheeseMVC.Data;
7+
using CheeseMVC.Models;
8+
using CheeseMVC.ViewModels;
9+
10+
// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
11+
12+
namespace CheeseMVC.Controllers
13+
{
14+
public class CategoryController : Controller
15+
16+
{
17+
private readonly CheeseDbContext context;
18+
19+
public CategoryController(CheeseDbContext dbContext)
20+
{
21+
context = dbContext;
22+
}
23+
24+
// GET: /<controller>/
25+
public IActionResult Index()
26+
27+
{
28+
List<CheeseCategory> categories = context.Categories.ToList();
29+
ViewBag.title = "Cheese Categories";
30+
ViewBag.categories = categories;
31+
32+
return View(categories);
33+
}
34+
public IActionResult Add()
35+
{
36+
AddCategoryViewModel addCategoryViewModel = new AddCategoryViewModel();
37+
38+
return View(addCategoryViewModel);
39+
}
40+
[HttpPost]
41+
public IActionResult Add(AddCategoryViewModel addCategoryViewModel)
42+
{
43+
if (ModelState.IsValid)
44+
{
45+
CheeseCategory newCategory = new CheeseCategory
46+
{
47+
Name = addCategoryViewModel.Name
48+
};
49+
context.Categories.Add(newCategory);
50+
context.SaveChanges();
51+
return Redirect("/Category");
52+
}
53+
return View(addCategoryViewModel);
54+
}
55+
}
56+
}

src/CheeseMVC/Data/CheeseDbContext.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
namespace CheeseMVC.Data
55
{
66
public class CheeseDbContext : DbContext
7+
78
{
9+
public DbSet<CheeseCategory> Categories { get; set; }
810
public DbSet<Cheese> Cheeses { get; set; }
911

1012
public CheeseDbContext(DbContextOptions<CheeseDbContext> options)

src/CheeseMVC/Migrations/20181209072136_AddCategory.Designer.cs

Lines changed: 55 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using Microsoft.EntityFrameworkCore.Metadata;
2+
using Microsoft.EntityFrameworkCore.Migrations;
3+
using System;
4+
using System.Collections.Generic;
5+
6+
namespace CheeseMVC.Migrations
7+
{
8+
public partial class AddCategory : Migration
9+
{
10+
protected override void Up(MigrationBuilder migrationBuilder)
11+
{
12+
migrationBuilder.CreateTable(
13+
name: "Categories",
14+
columns: table => new
15+
{
16+
ID = table.Column<int>(nullable: false)
17+
.Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
18+
Name = table.Column<string>(nullable: true)
19+
},
20+
constraints: table =>
21+
{
22+
table.PrimaryKey("PK_Categories", x => x.ID);
23+
});
24+
}
25+
26+
protected override void Down(MigrationBuilder migrationBuilder)
27+
{
28+
migrationBuilder.DropTable(
29+
name: "Categories");
30+
}
31+
}
32+
}

src/CheeseMVC/Migrations/CheeseDbContextModelSnapshot.cs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
using System;
1+
// <auto-generated />
2+
using CheeseMVC.Data;
3+
using CheeseMVC.Models;
24
using Microsoft.EntityFrameworkCore;
35
using Microsoft.EntityFrameworkCore.Infrastructure;
46
using Microsoft.EntityFrameworkCore.Metadata;
57
using Microsoft.EntityFrameworkCore.Migrations;
6-
using CheeseMVC.Data;
8+
using Microsoft.EntityFrameworkCore.Storage;
9+
using Microsoft.EntityFrameworkCore.Storage.Internal;
10+
using System;
711

812
namespace CheeseMVC.Migrations
913
{
@@ -12,8 +16,9 @@ partial class CheeseDbContextModelSnapshot : ModelSnapshot
1216
{
1317
protected override void BuildModel(ModelBuilder modelBuilder)
1418
{
19+
#pragma warning disable 612, 618
1520
modelBuilder
16-
.HasAnnotation("ProductVersion", "1.0.1")
21+
.HasAnnotation("ProductVersion", "2.0.1-rtm-125")
1722
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
1823

1924
modelBuilder.Entity("CheeseMVC.Models.Cheese", b =>
@@ -31,6 +36,19 @@ protected override void BuildModel(ModelBuilder modelBuilder)
3136

3237
b.ToTable("Cheeses");
3338
});
39+
40+
modelBuilder.Entity("CheeseMVC.Models.CheeseCategory", b =>
41+
{
42+
b.Property<int>("ID")
43+
.ValueGeneratedOnAdd();
44+
45+
b.Property<string>("Name");
46+
47+
b.HasKey("ID");
48+
49+
b.ToTable("Categories");
50+
});
51+
#pragma warning restore 612, 618
3452
}
3553
}
3654
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
6+
namespace CheeseMVC.Models
7+
{
8+
public class CheeseCategory
9+
{
10+
public int ID { get; set; }
11+
public string Name { get; set; }
12+
13+
public CheeseCategory() { }
14+
}
15+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel.DataAnnotations;
4+
using System.Linq;
5+
using System.Threading.Tasks;
6+
7+
namespace CheeseMVC.ViewModels
8+
{
9+
public class AddCategoryViewModel
10+
{
11+
[Required]
12+
[Display(Name="Category Name")]
13+
public String Name { get; set; }
14+
15+
public AddCategoryViewModel() { }
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
@model CheeseMVC.ViewModels.AddCategoryViewModel
2+
3+
<form asp-controller ="Category" asp-action ="Add" method="post">
4+
<br />
5+
6+
<label asp-for="Name"></label>
7+
<input asp-for= "Name"/>
8+
<span asp-validation-for="Name"></span>
9+
10+
11+
12+
13+
14+
15+
16+
17+
</form>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Mvc;
6+
using Microsoft.AspNetCore.Mvc.RazorPages;
7+
8+
namespace CheeseMVC.Views.Category
9+
{
10+
public class AddModel : PageModel
11+
{
12+
public void OnGet()
13+
{
14+
}
15+
}
16+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
@ViewBag.title
2+
3+
4+
<ul>
5+
@foreach (var category in ViewBag.categories)
6+
7+
{
8+
<li>@category.Name</li>
9+
}
10+
</ul>
11+
<p><a asp-controller= "Category" asp-action="Add">Add Category</a></p>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Mvc;
6+
using Microsoft.AspNetCore.Mvc.RazorPages;
7+
8+
namespace CheeseMVC.Views.Category
9+
{
10+
public class IndexModel : PageModel
11+
{
12+
public void OnGet()
13+
{
14+
}
15+
}
16+
}

src/CheeseMVC/Views/Shared/_Layout.cshtml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
<ul class="nav navbar-nav">
3333
<li><a asp-controller="Cheese" asp-action="Index">Home</a></li>
3434
<li><a asp-controller="Cheese" asp-action="Add">Add</a></li>
35+
<li><a asp-controller="Category" asp-action="Index">Categories</a></li>
3536
</ul>
3637
</div>
3738
</div>

src/CheeseMVC/web.config

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<configuration>
3-
43
<!--
54
Configure your application settings in appsettings.json. Learn more at http://go.microsoft.com/fwlink/?LinkId=786380
65
-->
7-
86
<system.webServer>
97
<handlers>
10-
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/>
8+
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
119
</handlers>
12-
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false"/>
10+
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false">
11+
<environmentVariables />
12+
</aspNetCore>
1313
</system.webServer>
14-
</configuration>
14+
</configuration>

0 commit comments

Comments
 (0)