Skip to content

Commit ba39c7b

Browse files
authored
Add Razor Pages example of contact list (#24)
1 parent c737ba3 commit ba39c7b

File tree

14 files changed

+584
-0
lines changed

14 files changed

+584
-0
lines changed

ASP.NET Core Basics/src/Contacts/Contacts.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<ItemGroup>
2121
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
2222
<PackageReference Include="BundlerMinifier.Core" Version="2.4.337" />
23+
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.0" />
2324
<PackageReference Include="Swashbuckle.AspNetCore" Version="1.0.0" />
2425
</ItemGroup>
2526

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
@page
2+
@model Contacts.Pages.ContactsRazorPages.CreateModel
3+
4+
@{
5+
ViewData["Title"] = "Create";
6+
Layout = "~/Views/Shared/_Layout.cshtml";
7+
}
8+
9+
<h2>Create</h2>
10+
11+
<h4>Contact</h4>
12+
<hr />
13+
<div class="row">
14+
<div class="col-md-4">
15+
<form method="post">
16+
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
17+
<div class="form-group">
18+
<label asp-for="Contact.Name" class="control-label"></label>
19+
<input asp-for="Contact.Name" class="form-control" />
20+
<span asp-validation-for="Contact.Name" class="text-danger"></span>
21+
</div>
22+
<div class="form-group">
23+
<label asp-for="Contact.Address" class="control-label"></label>
24+
<input asp-for="Contact.Address" class="form-control" />
25+
<span asp-validation-for="Contact.Address" class="text-danger"></span>
26+
</div>
27+
<div class="form-group">
28+
<label asp-for="Contact.City" class="control-label"></label>
29+
<input asp-for="Contact.City" class="form-control" />
30+
<span asp-validation-for="Contact.City" class="text-danger"></span>
31+
</div>
32+
<div class="form-group">
33+
<label asp-for="Contact.State" class="control-label"></label>
34+
<input asp-for="Contact.State" class="form-control" />
35+
<span asp-validation-for="Contact.State" class="text-danger"></span>
36+
</div>
37+
<div class="form-group">
38+
<label asp-for="Contact.PostalCode" class="control-label"></label>
39+
<input asp-for="Contact.PostalCode" class="form-control" />
40+
<span asp-validation-for="Contact.PostalCode" class="text-danger"></span>
41+
</div>
42+
<div class="form-group">
43+
<label asp-for="Contact.Phone" class="control-label"></label>
44+
<input asp-for="Contact.Phone" class="form-control" />
45+
<span asp-validation-for="Contact.Phone" class="text-danger"></span>
46+
</div>
47+
<div class="form-group">
48+
<label asp-for="Contact.Email" class="control-label"></label>
49+
<input asp-for="Contact.Email" class="form-control" />
50+
<span asp-validation-for="Contact.Email" class="text-danger"></span>
51+
</div>
52+
<div class="form-group">
53+
<input type="submit" value="Create" class="btn btn-default" />
54+
</div>
55+
</form>
56+
</div>
57+
</div>
58+
59+
<div>
60+
<a asp-page="Index">Back to List</a>
61+
</div>
62+
63+
@section Scripts {
64+
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
65+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
using Microsoft.AspNetCore.Mvc.Rendering;
8+
using Contacts.Data;
9+
using Contacts.Models;
10+
11+
namespace Contacts.Pages.ContactsRazorPages
12+
{
13+
public class CreateModel : PageModel
14+
{
15+
private readonly Contacts.Data.ContactsContext _context;
16+
17+
public CreateModel(Contacts.Data.ContactsContext context)
18+
{
19+
_context = context;
20+
}
21+
22+
public IActionResult OnGet()
23+
{
24+
return Page();
25+
}
26+
27+
[BindProperty]
28+
public Contact Contact { get; set; }
29+
30+
public async Task<IActionResult> OnPostAsync()
31+
{
32+
if (!ModelState.IsValid)
33+
{
34+
return Page();
35+
}
36+
37+
_context.Contact.Add(Contact);
38+
await _context.SaveChangesAsync();
39+
40+
return RedirectToPage("./Index");
41+
}
42+
}
43+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
@page
2+
@model Contacts.Pages.ContactsRazorPages.DeleteModel
3+
4+
@{
5+
ViewData["Title"] = "Delete";
6+
Layout = "~/Views/Shared/_Layout.cshtml";
7+
}
8+
9+
<h2>Delete</h2>
10+
11+
<h3>Are you sure you want to delete this?</h3>
12+
<div>
13+
<h4>Contact</h4>
14+
<hr />
15+
<dl class="dl-horizontal">
16+
<dt>
17+
@Html.DisplayNameFor(model => model.Contact.Name)
18+
</dt>
19+
<dd>
20+
@Html.DisplayFor(model => model.Contact.Name)
21+
</dd>
22+
<dt>
23+
@Html.DisplayNameFor(model => model.Contact.Address)
24+
</dt>
25+
<dd>
26+
@Html.DisplayFor(model => model.Contact.Address)
27+
</dd>
28+
<dt>
29+
@Html.DisplayNameFor(model => model.Contact.City)
30+
</dt>
31+
<dd>
32+
@Html.DisplayFor(model => model.Contact.City)
33+
</dd>
34+
<dt>
35+
@Html.DisplayNameFor(model => model.Contact.State)
36+
</dt>
37+
<dd>
38+
@Html.DisplayFor(model => model.Contact.State)
39+
</dd>
40+
<dt>
41+
@Html.DisplayNameFor(model => model.Contact.PostalCode)
42+
</dt>
43+
<dd>
44+
@Html.DisplayFor(model => model.Contact.PostalCode)
45+
</dd>
46+
<dt>
47+
@Html.DisplayNameFor(model => model.Contact.Phone)
48+
</dt>
49+
<dd>
50+
@Html.DisplayFor(model => model.Contact.Phone)
51+
</dd>
52+
<dt>
53+
@Html.DisplayNameFor(model => model.Contact.Email)
54+
</dt>
55+
<dd>
56+
@Html.DisplayFor(model => model.Contact.Email)
57+
</dd>
58+
</dl>
59+
60+
<form method="post">
61+
<input type="hidden" asp-for="Contact.Id" />
62+
<input type="submit" value="Delete" class="btn btn-default" /> |
63+
<a asp-page="./Index">Back to List</a>
64+
</form>
65+
</div>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
using Microsoft.EntityFrameworkCore;
8+
using Contacts.Data;
9+
using Contacts.Models;
10+
11+
namespace Contacts.Pages.ContactsRazorPages
12+
{
13+
public class DeleteModel : PageModel
14+
{
15+
private readonly Contacts.Data.ContactsContext _context;
16+
17+
public DeleteModel(Contacts.Data.ContactsContext context)
18+
{
19+
_context = context;
20+
}
21+
22+
[BindProperty]
23+
public Contact Contact { get; set; }
24+
25+
public async Task<IActionResult> OnGetAsync(int? id)
26+
{
27+
if (id == null)
28+
{
29+
return NotFound();
30+
}
31+
32+
Contact = await _context.Contact.SingleOrDefaultAsync(m => m.Id == id);
33+
34+
if (Contact == null)
35+
{
36+
return NotFound();
37+
}
38+
return Page();
39+
}
40+
41+
public async Task<IActionResult> OnPostAsync(int? id)
42+
{
43+
if (id == null)
44+
{
45+
return NotFound();
46+
}
47+
48+
Contact = await _context.Contact.FindAsync(id);
49+
50+
if (Contact != null)
51+
{
52+
_context.Contact.Remove(Contact);
53+
await _context.SaveChangesAsync();
54+
}
55+
56+
return RedirectToPage("./Index");
57+
}
58+
}
59+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
@page
2+
@model Contacts.Pages.ContactsRazorPages.DetailsModel
3+
4+
@{
5+
ViewData["Title"] = "Details";
6+
Layout = "~/Views/Shared/_Layout.cshtml";
7+
}
8+
9+
<h2>Details</h2>
10+
11+
<div>
12+
<h4>Contact</h4>
13+
<hr />
14+
<dl class="dl-horizontal">
15+
<dt>
16+
@Html.DisplayNameFor(model => model.Contact.Name)
17+
</dt>
18+
<dd>
19+
@Html.DisplayFor(model => model.Contact.Name)
20+
</dd>
21+
<dt>
22+
@Html.DisplayNameFor(model => model.Contact.Address)
23+
</dt>
24+
<dd>
25+
@Html.DisplayFor(model => model.Contact.Address)
26+
</dd>
27+
<dt>
28+
@Html.DisplayNameFor(model => model.Contact.City)
29+
</dt>
30+
<dd>
31+
@Html.DisplayFor(model => model.Contact.City)
32+
</dd>
33+
<dt>
34+
@Html.DisplayNameFor(model => model.Contact.State)
35+
</dt>
36+
<dd>
37+
@Html.DisplayFor(model => model.Contact.State)
38+
</dd>
39+
<dt>
40+
@Html.DisplayNameFor(model => model.Contact.PostalCode)
41+
</dt>
42+
<dd>
43+
@Html.DisplayFor(model => model.Contact.PostalCode)
44+
</dd>
45+
<dt>
46+
@Html.DisplayNameFor(model => model.Contact.Phone)
47+
</dt>
48+
<dd>
49+
@Html.DisplayFor(model => model.Contact.Phone)
50+
</dd>
51+
<dt>
52+
@Html.DisplayNameFor(model => model.Contact.Email)
53+
</dt>
54+
<dd>
55+
@Html.DisplayFor(model => model.Contact.Email)
56+
</dd>
57+
</dl>
58+
</div>
59+
<div>
60+
<a asp-page="./Edit" asp-route-id="@Model.Contact.Id">Edit</a> |
61+
<a asp-page="./Index">Back to List</a>
62+
</div>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
using Microsoft.EntityFrameworkCore;
8+
using Contacts.Data;
9+
using Contacts.Models;
10+
11+
namespace Contacts.Pages.ContactsRazorPages
12+
{
13+
public class DetailsModel : PageModel
14+
{
15+
private readonly Contacts.Data.ContactsContext _context;
16+
17+
public DetailsModel(Contacts.Data.ContactsContext context)
18+
{
19+
_context = context;
20+
}
21+
22+
public Contact Contact { get; set; }
23+
24+
public async Task<IActionResult> OnGetAsync(int? id)
25+
{
26+
if (id == null)
27+
{
28+
return NotFound();
29+
}
30+
31+
Contact = await _context.Contact.SingleOrDefaultAsync(m => m.Id == id);
32+
33+
if (Contact == null)
34+
{
35+
return NotFound();
36+
}
37+
return Page();
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)