public ActionResult Index(FormCollection form, string sortOrder, string searchString, string currentFilter, int?page) { using (DBModelEntity dbModel = new DBModelEntity()) { ViewBag.CurrentSort = sortOrder; ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : ""; ViewBag.ValueSortParm = sortOrder == "Value" ? "Value_desc" : "Value"; if (searchString != null) { page = 1; } else { searchString = currentFilter; } ViewBag.CurrentFilter = searchString; var items = from s in dbModel.Items select s; if (!String.IsNullOrEmpty(searchString)) { int result = 0; if (int.TryParse(searchString, out result)) { items = items.Where(s => s.Name.Contains(searchString) || s.Value.ToString().Contains(searchString)); } else { items = items.Where(s => s.Name.Contains(searchString)); } } switch (sortOrder) { case "name_desc": items = items.OrderByDescending(s => s.Name); break; case "Value": items = items.OrderBy(s => s.Value); break; case "Value_desc": items = items.OrderByDescending(s => s.Value); break; default: items = items.OrderBy(s => s.Id); break; } int pageSize = 5; int pageNumber = (page ?? 1); return(View(items.ToPagedList(pageNumber, pageSize))); //return View(items.ToList()); } }
public ActionResult Details(int id) { using (DBModelEntity dbModel = new DBModelEntity()) { return(View(dbModel.Items.Where(x => x.Id == id).FirstOrDefault())); } }
// GET: Item/Delete/5 public ActionResult Delete(int id) { if (!userIsAdmin()) { return(RedirectToAction("Login", "Login")); } using (DBModelEntity dbModel = new DBModelEntity()) { return(View(dbModel.Items.Where(x => x.Id == id).FirstOrDefault())); } }
public ActionResult UserProfile() { if (Session["userName"] == null) { return(RedirectToAction("Index", "Home")); } using (DBModelEntity dbModel = new DBModelEntity()) { string userName = this.HttpContext.Session["userName"].ToString(); return(View(dbModel.Users.Where(x => x.Name == userName).FirstOrDefault())); } }
public ActionResult AddOrEdit(Users user) { using (DBModelEntity dbmodel = new DBModelEntity()) { if (dbmodel.Users.Any(x => x.Name == user.Name)) { ViewBag.DuplicateMessage = "Username already exists"; return(View("AddOrEdit", user)); } dbmodel.Users.Add(user); dbmodel.SaveChanges(); } ModelState.Clear(); ViewBag.SuccessMessage = "Registration Successful."; return(View("AddOrEdit", new Users())); }
public ActionResult Create(Items item) { if (!userIsAdmin()) { return(RedirectToAction("Login", "Login")); } try { using (DBModelEntity dbModel = new DBModelEntity()) { dbModel.Items.Add(item); dbModel.SaveChanges(); } return(RedirectToAction("Index")); } catch { return(View()); } }
public ActionResult Edit(int id, Items item) { if (!userIsAdmin()) { return(RedirectToAction("Login", "Login")); } try { // TODO: Add update logic here using (DBModelEntity dbModel = new DBModelEntity()) { dbModel.Entry(item).State = EntityState.Modified; dbModel.SaveChanges(); } return(RedirectToAction("Index")); } catch { return(View()); } }
public ActionResult Delete(int id, FormCollection collection) { if (!userIsAdmin()) { return(RedirectToAction("Login", "Login")); } try { using (DBModelEntity dbModel = new DBModelEntity()) { Items item = dbModel.Items.Where(x => x.Id == id).FirstOrDefault(); dbModel.Items.Remove(item); dbModel.SaveChanges(); return(RedirectToAction("Index")); } } catch { return(View()); } }
// do with async? it should speed up the login process //public async Task<ActionResult> Authorize(Customers customer) public ActionResult Authorize(Users customer) { using (DBModelEntity db = new DBModelEntity()) { //var result = await _signInManager.Pass var userDetails = db.Users.Where(x => x.Name == customer.Name && x.Password == customer.Password).FirstOrDefault(); if (userDetails == null) { ViewBag.ErrorMessage = "Login error"; return(View("Login", customer)); } else { Session["userId"] = userDetails.Id; Session["userName"] = userDetails.Name; Session["userIsAdmin"] = userDetails.IsAdmin; HttpCookie UserCookie = new HttpCookie("user", userDetails.Id.ToString()); UserCookie.Expires.AddDays(10); HttpContext.Response.SetCookie(UserCookie); //HttpCookie NewCookie = Request.Cookies["user"]; //return NewCookie.Value; //rememberme always set to true for now FormsAuthentication.SetAuthCookie(userDetails.Name, true); return(RedirectToAction("Index", "Home")); } } //return View(); }