root/trunk/Wierszowki/Wierszowki.Web/Controllers/AuthorController.cs @ 752

Wersja 752, 3.1 KB (wprowadzona przez marek, 17 years temu)

wiersz

Line 
1using System.Linq;
2using System.Web.Mvc;
3using Wierszowki.Core.Linq;
4using Wierszowki.Models;
5using Wierszowki.Services;
6using xVal.ServerSide;
7
8namespace Wierszowki.Controllers
9{
10    public class AuthorController : Controller
11    {
12        private readonly WierszowkiDataContext _context;
13
14        public AuthorController()
15        {
16            _context = new WierszowkiDataContext();
17        }
18
19        public ActionResult Index()
20        {
21            var authors = _context.GetAuthors();
22            return View(authors);
23        }
24
25        [Authorize]
26        [AcceptVerbs(HttpVerbs.Get)]
27        public ActionResult Create()
28        {
29            var authorViewData = new AuthorViewData
30                                     {
31                                         Author = new Author(),
32                                         EmploymentTypeList = new SelectList(_context.GetEmploymentTypes().ToList(), "Id", "Name")
33                                     };
34
35            return View(authorViewData);
36        }
37
38        [Authorize]
39        [AcceptVerbs(HttpVerbs.Post)]
40        public ActionResult Create(Author author)
41        {
42            var authorService = new AuthorService();
43            try
44            {
45                authorService.Create(author);
46            }
47            catch (RulesException ex)
48            {
49                ex.AddModelStateErrors(ModelState, "author");
50            }
51
52            if (ModelState.IsValid)
53                return RedirectToAction("Confirm", author);
54
55
56            var authorViewData = new AuthorViewData
57            {
58                Author = author,
59                EmploymentTypeList = new SelectList(_context.GetEmploymentTypes().ToList(), "Id", "Name")
60            };
61            return View(authorViewData);
62        }
63
64
65        [Authorize]
66        public ActionResult Confirm(Author author)
67        {
68            return View(author);
69        }
70
71        [Authorize]
72        [AcceptVerbs(HttpVerbs.Get)]
73        public ActionResult Edit(int id)
74        {
75            var authorViewData = new AuthorViewData
76                     {
77                         Author = _context.GetAuthorById(id),
78                         EmploymentTypeList = new SelectList(_context.GetEmploymentTypes().ToList(), "Id", "Name")
79                     };
80
81            return View(authorViewData);
82        }
83
84        [Authorize]
85        [AcceptVerbs(HttpVerbs.Post)]
86        public ActionResult Edit(Author author)
87        {
88            var authorService = new AuthorService();
89            try
90            {
91                authorService.Update(author);
92            }
93            catch (RulesException ex)
94            {
95                ex.AddModelStateErrors(ModelState, "author");
96            }
97
98            if (ModelState.IsValid)
99                return View("Confirm", author);
100
101            var authorViewData = new AuthorViewData
102            {
103                Author = author,
104                EmploymentTypeList = new SelectList(_context.GetEmploymentTypes().ToList(), "Id", "Name")
105            };
106
107            return View(authorViewData);
108        }
109    }
110}
Notatka: Zobacz TracBrowser aby uzyskać więcej informacji.