Advertisement
Guest User

Untitled

a guest
May 18th, 2016
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.03 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using Automarket.Models;
  7. using System.Data.Entity.Spatial;
  8.  
  9. namespace Automarket.Controllers
  10. {
  11.  
  12. public class AccountController : Controller
  13. {
  14. // GET: Account
  15. public ActionResult Index()
  16. {
  17. using (OurDBContext db = new OurDBContext())
  18. {
  19. return View(db.userAccount.ToList());
  20. }
  21. }
  22.  
  23. public ActionResult Register()
  24. {
  25. return View();
  26. }
  27.  
  28. [HttpPost]
  29. public ActionResult Register(UserAccount account)
  30. {
  31. if (ModelState.IsValid)
  32. {
  33. using (OurDBContext db = new OurDBContext())
  34. {
  35. db.userAccount.Add(account);
  36. db.SaveChanges();
  37. }
  38. ModelState.Clear();
  39. ViewBag.Message = account.Firstname + " " + account.Lastname + "Succesfully Registered: ";
  40. }
  41. return View();
  42. }
  43. // Login method
  44. public ActionResult Login()
  45. {
  46. return View();
  47. }
  48.  
  49. [HttpPost]
  50.  
  51. public ActionResult Login(UserAccount user)
  52. {
  53. using (OurDBContext db = new OurDBContext())
  54. {
  55. var usr = db.userAccount.Single(u => u.Username == user.Username && u.Password == user.Password);
  56. if (usr != null)
  57. {
  58. Session["UserID"] = usr.Id.ToString();
  59. Session["Username"] = usr.Username.ToString();
  60. return RedirectToAction("Logged In ");
  61.  
  62. }
  63. else
  64. {
  65. ModelState.AddModelError(" ", "Username or Password are incoorect");
  66. }
  67. }
  68. return View();
  69. }
  70.  
  71.  
  72. public ActionResult LoggedIn()
  73. {
  74. if (Session["UserID"] != null)
  75. {
  76. return View();
  77. }
  78. else
  79. {
  80. return RedirectToAction("Login");
  81. }
  82.  
  83. }
  84. [HttpGet]
  85. public ActionResult Create()
  86. {
  87. return View();
  88. }
  89.  
  90. [HttpPost]
  91. public ActionResult Create(UserAccount account)
  92. {
  93. using (OurDBContext db = new OurDBContext())
  94. if (ModelState.IsValid)
  95. {
  96. db.userAccount.Add(account);
  97. return RedirectToAction("Index");
  98. }
  99. return View(account);
  100. }
  101.  
  102. // List the details of user
  103. public ActionResult Details(int id=0)
  104. {
  105. using (OurDBContext db = new OurDBContext())
  106. {
  107. UserAccount user = db.userAccount.Find(id);
  108. if (user==null)
  109. {
  110. return HttpNotFound();
  111. }
  112. return View(user);
  113. }
  114. }
  115.  
  116. // post method for Delete User
  117. public ActionResult Delete(int id =0)
  118. {
  119. using (OurDBContext db = new OurDBContext())
  120. {
  121. UserAccount user = db.userAccount.Find(id);
  122. if (user == null)
  123. {
  124. return HttpNotFound();
  125. }
  126. return View(user);
  127. }
  128. }
  129.  
  130. [HttpPost,ActionName("Delete")]
  131. public ActionResult DeleteConfirmed(int id=0)
  132. {
  133. using (OurDBContext db = new OurDBContext())
  134. {
  135. UserAccount user = db.userAccount.Find(id);
  136. if (user==null)
  137. {
  138. return HttpNotFound();
  139. }
  140. db.userAccount.Remove(user);
  141. db.SaveChanges();
  142. return RedirectToAction("Index");
  143. }
  144. }
  145.  
  146. public ActionResult Edit(int? id)
  147. {
  148. using (OurDBContext db = new OurDBContext())
  149. {
  150. if (id == null)
  151. {
  152. return new HttpStatusCodeResult(System.Net.HttpStatusCode.BadRequest);
  153. }
  154. UserAccount user = db.userAccount.Find(id);
  155. if (user == null)
  156. {
  157. return HttpNotFound();
  158. }
  159. return View(user);
  160. }
  161. }
  162.  
  163.  
  164. [HttpPost]
  165. [ValidateAntiForgeryToken]
  166. public ActionResult Edit([Bind(Include = "Id,Firstname,Lastname,Email,Username,Password,ConfirmaPassword")] UserAccount user)
  167. {
  168. using (OurDBContext db = new OurDBContext())
  169. if (ModelState.IsValid)
  170. {
  171. db.Entry(user).State = System.Data.Entity.EntityState.Modified;
  172. db.SaveChanges();
  173. return RedirectToAction("Index");
  174. }
  175. return View(user);
  176. }
  177.  
  178. }
  179. }
  180.  
  181. @model IEnumerable<Automarket.Models.UserAccount>
  182.  
  183. @{
  184. ViewBag.Title = "Index";
  185. }
  186.  
  187.  
  188.  
  189. <h2>Index</h2>
  190.  
  191. <p>
  192. @Html.ActionLink("Create New", "Register")
  193.  
  194. </p>
  195. <table class="table">
  196. <tr>
  197. <th>
  198. @Html.DisplayNameFor(model => model.Firstname)
  199. </th>
  200. <th>
  201. @Html.DisplayNameFor(model => model.Lastname)
  202. </th>
  203. <th>
  204. @Html.DisplayNameFor(model => model.Email)
  205. </th>
  206. <th>
  207. @Html.DisplayNameFor(model => model.Username)
  208. </th>
  209.  
  210. <th></th>
  211. </tr>
  212.  
  213. @foreach (var item in Model) {
  214. <tr>
  215. <td>
  216. @Html.DisplayFor(modelItem => item.Firstname)
  217. </td>
  218. <td>
  219. @Html.DisplayFor(modelItem => item.Lastname)
  220. </td>
  221. <td>
  222. @Html.DisplayFor(modelItem => item.Email)
  223. </td>
  224. <td>
  225. @Html.DisplayFor(modelItem => item.Username)
  226. </td>
  227.  
  228. <td>
  229. @Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
  230. @Html.ActionLink("Details", "Details", new { id=item.Id }) |
  231. @Html.ActionLink("Delete", "Delete", new { id=item.Id })
  232. </td>
  233. </tr>
  234. }
  235.  
  236. </table>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement