Skip to content

Commit 3fe11c0

Browse files
committed
Add contacts controller and related views.
1 parent 8ac8aed commit 3fe11c0

File tree

14 files changed

+672
-1
lines changed

14 files changed

+672
-1
lines changed
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Contacts.Data;
6+
using Microsoft.AspNetCore.Mvc;
7+
using Microsoft.AspNetCore.Mvc.Rendering;
8+
using Microsoft.EntityFrameworkCore;
9+
using Contacts.Models;
10+
11+
namespace Contacts.Controllers
12+
{
13+
public class ContactsController : Controller
14+
{
15+
private readonly ContactsContext _context;
16+
17+
public ContactsController(ContactsContext context)
18+
{
19+
_context = context;
20+
}
21+
22+
// GET: Contacts
23+
public async Task<IActionResult> Index()
24+
{
25+
return View(await _context.Contact.ToListAsync());
26+
}
27+
28+
// GET: Contacts/Details/5
29+
public async Task<IActionResult> Details(int? id)
30+
{
31+
if (id == null)
32+
{
33+
return NotFound();
34+
}
35+
36+
var contact = await _context.Contact.SingleOrDefaultAsync(m => m.Id == id);
37+
if (contact == null)
38+
{
39+
return NotFound();
40+
}
41+
42+
return View(contact);
43+
}
44+
45+
// GET: Contacts/Create
46+
public IActionResult Create()
47+
{
48+
return View();
49+
}
50+
51+
// POST: Contacts/Create
52+
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
53+
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
54+
[HttpPost]
55+
[ValidateAntiForgeryToken]
56+
public async Task<IActionResult> Create([Bind("Id,Address,City,Email,Name,Phone,PostalCode,State")] Contact contact)
57+
{
58+
if (ModelState.IsValid)
59+
{
60+
_context.Add(contact);
61+
await _context.SaveChangesAsync();
62+
return RedirectToAction("Index");
63+
}
64+
return View(contact);
65+
}
66+
67+
// GET: Contacts/Edit/5
68+
public async Task<IActionResult> Edit(int? id)
69+
{
70+
if (id == null)
71+
{
72+
return NotFound();
73+
}
74+
75+
var contact = await _context.Contact.SingleOrDefaultAsync(m => m.Id == id);
76+
if (contact == null)
77+
{
78+
return NotFound();
79+
}
80+
return View(contact);
81+
}
82+
83+
// POST: Contacts/Edit/5
84+
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
85+
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
86+
[HttpPost]
87+
[ValidateAntiForgeryToken]
88+
public async Task<IActionResult> Edit(int id, [Bind("Id,Address,City,Email,Name,Phone,PostalCode,State")] Contact contact)
89+
{
90+
if (id != contact.Id)
91+
{
92+
return NotFound();
93+
}
94+
95+
if (ModelState.IsValid)
96+
{
97+
try
98+
{
99+
_context.Update(contact);
100+
await _context.SaveChangesAsync();
101+
}
102+
catch (DbUpdateConcurrencyException)
103+
{
104+
if (!ContactExists(contact.Id))
105+
{
106+
return NotFound();
107+
}
108+
else
109+
{
110+
throw;
111+
}
112+
}
113+
return RedirectToAction("Index");
114+
}
115+
return View(contact);
116+
}
117+
118+
// GET: Contacts/Delete/5
119+
public async Task<IActionResult> Delete(int? id)
120+
{
121+
if (id == null)
122+
{
123+
return NotFound();
124+
}
125+
126+
var contact = await _context.Contact.SingleOrDefaultAsync(m => m.Id == id);
127+
if (contact == null)
128+
{
129+
return NotFound();
130+
}
131+
132+
return View(contact);
133+
}
134+
135+
// POST: Contacts/Delete/5
136+
[HttpPost, ActionName("Delete")]
137+
[ValidateAntiForgeryToken]
138+
public async Task<IActionResult> DeleteConfirmed(int id)
139+
{
140+
var contact = await _context.Contact.SingleOrDefaultAsync(m => m.Id == id);
141+
_context.Contact.Remove(contact);
142+
await _context.SaveChangesAsync();
143+
return RedirectToAction("Index");
144+
}
145+
146+
private bool ContactExists(int id)
147+
{
148+
return _context.Contact.Any(e => e.Id == id);
149+
}
150+
}
151+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using Contacts.Models;
2+
using Microsoft.EntityFrameworkCore;
3+
4+
namespace Contacts.Data
5+
{
6+
public sealed class ContactsContext : DbContext
7+
{
8+
private static bool _created;
9+
public DbSet<Contact> Contact { get; set; }
10+
public ContactsContext(DbContextOptions<ContactsContext> options)
11+
: base(options)
12+
{
13+
if (_created) return;
14+
Database.Migrate();
15+
_created = true;
16+
}
17+
}
18+
}

ASP.NET Core Basics/src/Contacts/Migrations/20160804115014_Init-Contacts.Designer.cs

Lines changed: 46 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Microsoft.EntityFrameworkCore.Migrations;
4+
using Microsoft.EntityFrameworkCore.Metadata;
5+
6+
namespace Contacts.Migrations
7+
{
8+
public partial class InitContacts : Migration
9+
{
10+
protected override void Up(MigrationBuilder migrationBuilder)
11+
{
12+
migrationBuilder.CreateTable(
13+
name: "Contact",
14+
columns: table => new
15+
{
16+
Id = table.Column<int>(nullable: false)
17+
.Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
18+
Address = table.Column<string>(nullable: true),
19+
City = table.Column<string>(nullable: true),
20+
Email = table.Column<string>(nullable: true),
21+
Name = table.Column<string>(nullable: true),
22+
Phone = table.Column<string>(nullable: true),
23+
PostalCode = table.Column<string>(nullable: true),
24+
State = table.Column<string>(nullable: true)
25+
},
26+
constraints: table =>
27+
{
28+
table.PrimaryKey("PK_Contact", x => x.Id);
29+
});
30+
}
31+
32+
protected override void Down(MigrationBuilder migrationBuilder)
33+
{
34+
migrationBuilder.DropTable(
35+
name: "Contact");
36+
}
37+
}
38+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
using Contacts.Data;
3+
using Microsoft.EntityFrameworkCore;
4+
using Microsoft.EntityFrameworkCore.Infrastructure;
5+
using Microsoft.EntityFrameworkCore.Metadata;
6+
using Microsoft.EntityFrameworkCore.Migrations;
7+
using Contacts.Models;
8+
9+
namespace Contacts.Migrations
10+
{
11+
[DbContext(typeof(ContactsContext))]
12+
partial class ContactsContextModelSnapshot : ModelSnapshot
13+
{
14+
protected override void BuildModel(ModelBuilder modelBuilder)
15+
{
16+
modelBuilder
17+
.HasAnnotation("ProductVersion", "1.0.0-rtm-21431")
18+
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
19+
20+
modelBuilder.Entity("Contacts.Models.Contact", b =>
21+
{
22+
b.Property<int>("Id")
23+
.ValueGeneratedOnAdd();
24+
25+
b.Property<string>("Address");
26+
27+
b.Property<string>("City");
28+
29+
b.Property<string>("Email");
30+
31+
b.Property<string>("Name");
32+
33+
b.Property<string>("Phone");
34+
35+
b.Property<string>("PostalCode");
36+
37+
b.Property<string>("State");
38+
39+
b.HasKey("Id");
40+
41+
b.ToTable("Contact");
42+
});
43+
}
44+
}
45+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace Contacts.Models
2+
{
3+
public class Contact
4+
{
5+
public int Id { get; set; }
6+
public string Name { get; set; }
7+
public string Address { get; set; }
8+
public string City { get; set; }
9+
public string State { get; set; }
10+
public string PostalCode { get; set; }
11+
public string Phone { get; set; }
12+
public string Email { get; set; }
13+
}
14+
}

ASP.NET Core Basics/src/Contacts/Startup.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ public void ConfigureServices(IServiceCollection services)
5252
// Add application services.
5353
services.AddTransient<IEmailSender, AuthMessageSender>();
5454
services.AddTransient<ISmsSender, AuthMessageSender>();
55+
56+
services.AddDbContext<ContactsContext>(options =>
57+
options.UseSqlServer(Configuration["Data:ContactsContext:ConnectionString"]));
5558
}
5659

5760
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

0 commit comments

Comments
 (0)