codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
//CONTROLLER using OnlineShop.DAO; using OnlineShop.EF; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using PagedList; namespace OnlineShop.Areas.Admin.Controllers { public class UserController : BaseController { // GET: Admin/User public ActionResult Index(int Page=1,int Pagesize=1)//Mot Ban ghi 1 trang { var model = new UserDao(); return View(model.ShowAllUser()); } [HttpGet] public ActionResult Create() { return View(); } [HttpPost] public ActionResult Create(User x) { if(ModelState.IsValid) { var dao = new UserDao(); var MD5 = Commond.Encrytor.MD5Hash(x.Password); x.Password = MD5; long id =dao.InsertUser(x); if(id>0) { return RedirectToAction("Index", "User"); } else { ModelState.AddModelError("", "TheM That bai!"); } } return View("Index"); } public ActionResult Edit(int id=1) { var Model = new UserDao(); Model.GetUserFollowId(id); return View(Model); } } } //Modelusing OnlineShop.EF; using System; using System.Collections.Generic; using System.Linq; using System.Web; using PagedList; namespace OnlineShop.DAO { public class UserDao { OnlineDB DB = null; public UserDao() { DB = new OnlineDB(); } //Method ShowAll List User public IEnumerable<User> ShowAllUser() { var item = from i in DB.Users select i; return item; } public User GetUserFollowId(int id) { return DB.Users.Find(id); } //Update public bool UpdateOneUser(User Etity) { try { var User = DB.Users.Find(Etity.ID); User.Password = Etity.Password; User.Status = Etity.Status; User.UserName = Etity.UserName; DB.SaveChanges(); } catch(Exception e) { return false; } return true; } //Method Inser user public long InsertUser(User Entity) { DB.Users.Add(Entity); DB.SaveChanges(); return Entity.ID; } //Test Login valid public string LoginValid(string userName,string password) { var result = DB.Users.SingleOrDefault(x=>x.UserName==userName);//Tra ve 1duy nhat 1 record if(result!=null) { if(result.Password!=password) { return " Password Invalid!"; } else if (result.Status == false) { return " Acount Is clocked!"; } else { return "Sussecfully!"; } } return "UserName Invalid!"; } public User GetByID(string username) { return DB.Users.SingleOrDefault(x => x.UserName == username); } } } //View @{ ViewBag.Title = "Index"; Layout = "~/Areas/Admin/Views/Shared/_LayoutAdmin.cshtml"; } @model OnlineShop.EF.User <h2>Edit Member</h2> @using (Html.BeginForm("Edit", "User", FormMethod.Post)) { @Html.AntiForgeryToken() <div class="form-horizontal"> <h4>User</h4> <hr /> @Html.ValidationSummary(true, "", new { @class = "text-danger" }) <div class="form-group"> @Html.LabelFor(model => model.UserName, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.TextBoxFor(model => model.UserName, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.UserName, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Password, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.TextBoxFor(model => model.Password, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Status, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Status, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Status, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> <div> <button type="submit" class="btn btn-default">Tạo</button> </div> </div> </div> } <div> @Html.ActionLink("Back to List", "Index") </div>
Private
[
?
]
Run code
Submit