Exemplo n.º 1
0
        public ActionResult Index(usersViewModel viewModel)
        {
            ActionResult ar;
            var          multiSelect = Request.Form["multiSelect"];
            // multi select
            tblUser tu = new tblUser();

            viewModel.clearMsg();
            switch (viewModel.cmd)
            {
            case "query":
                viewModel.errorMsg = loadUsers(ref viewModel);
                ar = View(viewModel);
                break;

            case "add":
                userEditViewModel tmpVMa = new userEditViewModel();
                tmpVMa.pageStatus             = (int)PAGE_STATUS.ADD;
                TempData["userEditViewModel"] = tmpVMa;
                ar = RedirectToAction("AddUpdateUser");
                return(ar);

            case "update":
                user u = tu.getById(viewModel.singleSelect);
                if (u != null)
                {
                    userEditViewModel tmpVM = new userEditViewModel();
                    tmpVM.editModel  = u;
                    tmpVM.pageStatus = (int)PAGE_STATUS.EDIT;
                    TempData["userEditViewModel"] = tmpVM;
                    ar = RedirectToAction("AddUpdateUser");
                    return(ar);
                }
                viewModel.errorMsg = "error reading this user";
                ar = View(viewModel);
                break;

            case "delete":
                if (string.IsNullOrWhiteSpace(multiSelect))
                {
                    viewModel.errorMsg = "please select user(s) to delete";
                }
                else
                {
                    string[] selected = multiSelect.Split(',');
                    foreach (string userId in selected.ToList())
                    {
                        viewModel.errorMsg += tu.Delete(userId);
                    }
                    tu.SaveChanges();
                    if (string.IsNullOrWhiteSpace(viewModel.errorMsg))
                    {
                        viewModel.successMsg = "successfully deleted";
                    }
                }
                loadUsers(ref viewModel);
                ar = View(viewModel);
                break;

            default:
                ar = View(viewModel);
                break;
            }
            return(ar);
        }
Exemplo n.º 2
0
        public ActionResult AddUpdateUser(userEditViewModel viewModel)
        {
            ActionResult ar;
            string       err;

            switch (viewModel.cmd)
            {
            case "save":
                err = checkForm(viewModel);
                if (err.Length > 0)
                {
                    viewModel.errorMsg = err;
                    ar = View(viewModel);
                    break;
                }
                tblUser tu = new tblUser();
                if (viewModel.pageStatus == (int)PAGE_STATUS.ADD)
                {
                    viewModel.editModel.createtime = DateTime.Now;
                    viewModel.editModel.modifytime = DateTime.Now;
                    err += tu.Add(viewModel.editModel);
                    err += tu.SaveChanges();
                    if (err.Length == 0)
                    {
                        viewModel.successMsg = "new user saved";
                        viewModel.pageStatus = (int)PAGE_STATUS.ADDSAVED;
                    }
                    else
                    {
                        viewModel.errorMsg = err;
                    }
                }
                else if (viewModel.pageStatus == (int)PAGE_STATUS.EDIT)
                {
                    viewModel.editModel.modifytime = DateTime.Now;
                    err += tu.Update(viewModel.editModel);
                    err += tu.SaveChanges();
                    if (err.Length == 0)
                    {
                        viewModel.successMsg = "user updated";
                        viewModel.pageStatus = (int)PAGE_STATUS.SAVED;
                    }
                    else
                    {
                        viewModel.errorMsg = err;
                    }
                }
                else
                {
                    viewModel.errorMsg = "wrong page status " + viewModel.pageStatus;
                }
                ar = View(viewModel);
                break;

            case "addNew":
                userEditViewModel tmpVMa = new userEditViewModel();
                tmpVMa.pageStatus             = (int)PAGE_STATUS.ADD;
                TempData["userEditViewModel"] = tmpVMa;
                ar = RedirectToAction("AddUpdateUser");
                return(ar);

            case "query":
                ar = RedirectToAction("Index");
                return(ar);

            default:
                ar = View(viewModel);
                break;
            }
            return(ar);
        }