public async Task <IActionResult> ProfileAsync(int id) { var item = await _context.Staffs.Include(d => d.Organization).FirstOrDefaultAsync(d => d.Id == id); if (item == null) { return(NotFound()); } var staffs = await _context.Staffs.Where(d => d.Active == true && d.OrganizationId == item.OrganizationId) .OrderByDescending(d => d.Importance).ThenBy(d => d.Id).ProjectTo <StaffVM>(_mapper.ConfigurationProvider).Take(3).ToListAsync(); var url = Request.Path.ToString(); ViewData["PageMeta"] = await _context.PageMetas.FirstOrDefaultAsync(d => d.ObjectId == url && d.ModuleType == (short)ModuleType.STAFF); var vm = new StaffDetailVM { StaffDetail = item, Staffs = staffs }; return(View(vm)); }
public ActionResult Edit(string id, [Bind(Include = "businessUnitId,staffCode,firstName,middleName,lastName,dob,startDate,profile,emailAddress")] StaffDetailVM staffVM) { if (ModelState.IsValid) { var efmodel = db.Staffs.FirstOrDefault(s => s.staffCode.Equals(staffVM.staffCode, StringComparison.OrdinalIgnoreCase) && s.Active == true); // Gets the business unit where the code equals the ID from the URL, regardless of case - equals null if not found var model = StaffDetailVM.buildModel(staffVM, efmodel); // Turns the view model into an edited version of the raw data model db.Entry(model).State = EntityState.Modified; // Tells the database context that the model is being updated db.SaveChanges(); // Saves changes to the database return(RedirectToAction("Index")); // Redirects to the listing of BusinessUnits } ViewBag.businessUnitId = new SelectList(db.BusinessUnits.Where(b => b.Active == true), "businessUnitId", "businessUnitCode", staffVM.businessUnitId); // Returns a list of business codes that are active return(View(staffVM)); }
// GET: Staffs/Details/5 public ActionResult Details(string id) { if (String.IsNullOrEmpty(id)) { throw new HttpException(400, "Bad Request"); // If an ID isn't provided in the URL, a HTTP 400 exception is thrown } var thisStaff = db.Staffs.FirstOrDefault(s => s.staffCode.Equals(id, StringComparison.OrdinalIgnoreCase) && s.Active == true); // Gets the staff member where the code equals the ID from the URL, regardless of case, and isn't soft deleted - equals null if not found if (thisStaff == null) { throw new HttpException(404, "Not Found"); // If the staff member doesn't exist for the given ID, a HTTP 404 exception is thrown } else { var viewModel = StaffDetailVM.buildViewModel(thisStaff); // Passes the staff member to the view model to get an object with formatted data return(View(viewModel)); // Passes the formatted business unit to the Razor view engine to render to the screen, using /Views/Staffs/Details.cshtml } }
public ActionResult Create([Bind(Include = "businessUnitId,staffCode,firstName,middleName,lastName,dob,startDate,profile,emailAddress")] Task1Start.Models.StaffDetailVM staffVM) { ViewBag.businessUnitId = new SelectList(db.BusinessUnits.Where(b => b.Active == true), "businessUnitId", "businessUnitCode"); // Returns a list of business codes that are active if (ModelState.IsValid) // If validation checks pass... { if (db.Staffs.Count(s => s.staffCode.Equals(staffVM.staffCode, StringComparison.OrdinalIgnoreCase) && s.Active == true) > 0) { ViewBag.Message = "The staff code is already in use!"; return(View(staffVM)); } else { var model = StaffDetailVM.buildModel(staffVM); // Passes the view model data and gets back a Staff model model.Active = true; // Sets the active flag to true (it's not been soft deleted!) db.Staffs.Add(model); // Inserts the data to the database as a new row db.SaveChanges(); // Saves the changes to the database return(RedirectToAction("Index")); // Redirects to the Staff list } } return(View(staffVM)); // Returns back to the creation form with the errors from validation }