-
Notifications
You must be signed in to change notification settings - Fork 163
Expand file tree
/
Copy pathContactsController.cs
More file actions
102 lines (86 loc) · 2.39 KB
/
Copy pathContactsController.cs
File metadata and controls
102 lines (86 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SharpRepository.Repository;
using SharpRepository.Samples.Core3Mvc.Models;
namespace SharpRepository.Samples.Core3Mvc.Controllers
{
public class ContactsController : Controller
{
protected IRepository<Contact, string> repository;
public ContactsController(IRepository<Contact, string> repository)
{
this.repository = repository;
}
// GET: Contacts
public ActionResult Index()
{
var contacts = repository.GetAll();
return View(contacts);
}
// GET: Contacts/Details/5
public ActionResult Details(string id)
{
var contact = repository.Get(id, "Emails");
return View(contact);
}
// GET: Contacts/Create
public ActionResult Create()
{
return View();
}
// POST: Contacts/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Contact contact)
{
repository.Add(contact);
return RedirectToAction(nameof(Index));
}
// GET: Contacts/Edit/5
public ActionResult Edit(string id)
{
var contact = repository.Get(id);
return View(contact);
}
// POST: Contacts/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(string id, Contact contact)
{
try
{
repository.Update(contact);
return RedirectToAction(nameof(Index));
}
catch
{
return View(contact);
}
}
// GET: Contacts/Delete/5
public ActionResult Delete(string id)
{
var contact = repository.Get(id);
return View(contact);
}
// POST: Contacts/Delete/5
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Delete(string id, IFormCollection collection)
{
try
{
repository.Delete(id);
return RedirectToAction(nameof(Index));
}
catch
{
return View();
}
}
}
}