Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Home Controllers
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using FinalExaml.Models;
- using System.Net;
- using System.Data.Entity;
- namespace FinalExaml.Controllers
- {
- public class HomeController : Controller
- {
- BookmanagerEntities2 db = new BookmanagerEntities2();
- // GET: Home
- public ActionResult Index()
- {
- return View(db.books.ToList().OrderBy(n=>n.price));
- }
- public ActionResult Details(int? id)
- {
- if (id == null)
- {
- return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
- }
- book bk = db.books.Find(id);
- if (bk == null)
- {
- return HttpNotFound();
- }
- return View(bk);
- }
- public ActionResult create()
- {
- return View();
- }
- [HttpPost]
- [ValidateAntiForgeryToken]
- public ActionResult create([Bind(Include = "bookID,bookName,publish,avatar,content,price")] book bk)
- {
- if (ModelState.IsValid)
- {
- db.books.Add(bk);
- db.SaveChanges();
- return RedirectToAction("Index");
- }
- return View(bk);
- }
- public ActionResult Edit(int? id)
- {
- if (id == null)
- {
- return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
- }
- book bk = db.books.Find(id);
- if (bk == null)
- {
- return HttpNotFound();
- }
- return View(bk);
- }
- [HttpPost]
- [ValidateAntiForgeryToken]
- public ActionResult Edit([Bind(Include = "bookID,bookName,publish,avatar,content,price")]book bk)
- {
- if (ModelState.IsValid)
- {
- db.Entry(bk).State = EntityState.Modified;
- db.SaveChanges();
- return RedirectToAction("Index");
- }
- return View(bk);
- }
- public ActionResult Delete(int? id)
- {
- if (id == null)
- {
- return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
- }
- book bk = db.books.Find(id);
- if (bk == null)
- {
- return HttpNotFound();
- }
- return View(bk);
- }
- [HttpPost]
- [ValidateAntiForgeryToken]
- public ActionResult Delete(int id)
- {
- book bk = db.books.Find(id);
- db.books.Remove(bk);
- db.SaveChanges();
- return RedirectToAction("Index");
- }
- }
- }
- Search Controllers
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using FinalExaml.Models;
- namespace FinalExaml.Controllers
- {
- public class SearchController : Controller
- {
- BookmanagerEntities2 db = new BookmanagerEntities2();
- // GET: Search
- [HttpGet]
- public ActionResult Index(string keyword)
- {
- var list = db.books.Where(n => n.bookName.Contains(keyword));
- return View(list.ToList());
- }
- [HttpPost]
- public ActionResult Index(FormCollection f)
- {
- var keyword = f["txtsearch"];
- var list = db.books.Where(n => n.bookName.Contains(keyword));
- return View(list.ToList());
- }
- }
- }
- User Controllers
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using FinalExaml.Models;
- namespace FinalExaml.Controllers
- {
- public class UserController : Controller
- {
- BookmanagerEntities2 db = new BookmanagerEntities2();
- // GET: User
- public ActionResult Index()
- {
- return View();
- }
- [HttpGet]
- public ActionResult Register()
- {
- return View();
- }
- [HttpPost]
- [ValidateAntiForgeryToken]
- public ActionResult Register([Bind(Include = "username,password,email")]User us)
- {
- if (ModelState.IsValid)
- {
- db.Users.Add(us);
- db.SaveChanges();
- return RedirectToAction("Index", "Home");
- }
- return View();
- }
- [HttpGet]
- public ActionResult Login()
- {
- return View();
- }
- [HttpPost]
- public ActionResult Login(string user,string pass)
- {
- user = Request.Form["txtUser"];
- pass = Request.Form["txtPass"];
- User us = db.Users.SingleOrDefault(n => n.username == user && n.password == pass);
- if (us != null)
- {
- Session["dn"] = us;
- return RedirectToAction("Index", "Home");
- }
- else
- {
- ViewBag.thongbao = "Đăng nhập thất bại";
- }
- return View();
- }
- public ActionResult Logout()
- {
- if(Session["dn"] != null)
- {
- Session["dn"] = null;
- }
- return RedirectToAction("Index", "Home");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement