Advertisement
aloner2712

Bai tap

Dec 24th, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.38 KB | None | 0 0
  1. Home Controllers
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Web.Mvc;
  7. using FinalExaml.Models;
  8. using System.Net;
  9. using System.Data.Entity;
  10.  
  11. namespace FinalExaml.Controllers
  12. {
  13. public class HomeController : Controller
  14. {
  15. BookmanagerEntities2 db = new BookmanagerEntities2();
  16. // GET: Home
  17. public ActionResult Index()
  18. {
  19. return View(db.books.ToList().OrderBy(n=>n.price));
  20. }
  21.  
  22. public ActionResult Details(int? id)
  23. {
  24. if (id == null)
  25. {
  26. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  27. }
  28. book bk = db.books.Find(id);
  29. if (bk == null)
  30. {
  31. return HttpNotFound();
  32. }
  33. return View(bk);
  34. }
  35.  
  36. public ActionResult create()
  37. {
  38. return View();
  39. }
  40. [HttpPost]
  41. [ValidateAntiForgeryToken]
  42. public ActionResult create([Bind(Include = "bookID,bookName,publish,avatar,content,price")] book bk)
  43. {
  44. if (ModelState.IsValid)
  45. {
  46. db.books.Add(bk);
  47. db.SaveChanges();
  48. return RedirectToAction("Index");
  49. }
  50. return View(bk);
  51. }
  52. public ActionResult Edit(int? id)
  53. {
  54. if (id == null)
  55. {
  56. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  57. }
  58. book bk = db.books.Find(id);
  59. if (bk == null)
  60. {
  61. return HttpNotFound();
  62. }
  63. return View(bk);
  64. }
  65. [HttpPost]
  66. [ValidateAntiForgeryToken]
  67. public ActionResult Edit([Bind(Include = "bookID,bookName,publish,avatar,content,price")]book bk)
  68. {
  69. if (ModelState.IsValid)
  70. {
  71. db.Entry(bk).State = EntityState.Modified;
  72. db.SaveChanges();
  73. return RedirectToAction("Index");
  74. }
  75. return View(bk);
  76. }
  77. public ActionResult Delete(int? id)
  78. {
  79. if (id == null)
  80. {
  81. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  82. }
  83.  
  84. book bk = db.books.Find(id);
  85. if (bk == null)
  86. {
  87. return HttpNotFound();
  88. }
  89. return View(bk);
  90. }
  91. [HttpPost]
  92. [ValidateAntiForgeryToken]
  93. public ActionResult Delete(int id)
  94. {
  95. book bk = db.books.Find(id);
  96. db.books.Remove(bk);
  97. db.SaveChanges();
  98. return RedirectToAction("Index");
  99. }
  100. }
  101. }
  102.  
  103.  
  104. Search Controllers
  105.  
  106. using System;
  107. using System.Collections.Generic;
  108. using System.Linq;
  109. using System.Web;
  110. using System.Web.Mvc;
  111. using FinalExaml.Models;
  112.  
  113. namespace FinalExaml.Controllers
  114. {
  115. public class SearchController : Controller
  116. {
  117. BookmanagerEntities2 db = new BookmanagerEntities2();
  118. // GET: Search
  119. [HttpGet]
  120. public ActionResult Index(string keyword)
  121. {
  122. var list = db.books.Where(n => n.bookName.Contains(keyword));
  123. return View(list.ToList());
  124. }
  125. [HttpPost]
  126. public ActionResult Index(FormCollection f)
  127. {
  128. var keyword = f["txtsearch"];
  129. var list = db.books.Where(n => n.bookName.Contains(keyword));
  130. return View(list.ToList());
  131. }
  132. }
  133. }
  134.  
  135. User Controllers
  136.  
  137. using System;
  138. using System.Collections.Generic;
  139. using System.Linq;
  140. using System.Web;
  141. using System.Web.Mvc;
  142. using FinalExaml.Models;
  143. namespace FinalExaml.Controllers
  144. {
  145. public class UserController : Controller
  146. {
  147. BookmanagerEntities2 db = new BookmanagerEntities2();
  148. // GET: User
  149. public ActionResult Index()
  150. {
  151. return View();
  152. }
  153. [HttpGet]
  154. public ActionResult Register()
  155. {
  156. return View();
  157. }
  158. [HttpPost]
  159. [ValidateAntiForgeryToken]
  160. public ActionResult Register([Bind(Include = "username,password,email")]User us)
  161. {
  162. if (ModelState.IsValid)
  163. {
  164. db.Users.Add(us);
  165. db.SaveChanges();
  166. return RedirectToAction("Index", "Home");
  167. }
  168. return View();
  169. }
  170.  
  171. [HttpGet]
  172. public ActionResult Login()
  173. {
  174. return View();
  175. }
  176. [HttpPost]
  177. public ActionResult Login(string user,string pass)
  178. {
  179. user = Request.Form["txtUser"];
  180. pass = Request.Form["txtPass"];
  181. User us = db.Users.SingleOrDefault(n => n.username == user && n.password == pass);
  182. if (us != null)
  183. {
  184. Session["dn"] = us;
  185. return RedirectToAction("Index", "Home");
  186. }
  187. else
  188. {
  189. ViewBag.thongbao = "Đăng nhập thất bại";
  190. }
  191. return View();
  192. }
  193. public ActionResult Logout()
  194. {
  195. if(Session["dn"] != null)
  196. {
  197. Session["dn"] = null;
  198. }
  199. return RedirectToAction("Index", "Home");
  200. }
  201. }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement