Ejemplo n.º 1
0
        public ActionResult Create(CreateLeaveTypeViewModel data)
        {
            try
            {
                if (!ModelState.IsValid) // ModelState being the model (data) that we get in the POST request - is a CreateLeaveTypeViewModel object in this case
                {
                    return(View(data));
                }

                var leaveType = _mapper.Map <LeaveType>(data); // mapping object of type CreateLeaveTypeViewModel into the type LeaveType (remember we want to map back to the data class before inserting into the db)
                leaveType.DateCreated = DateTime.Now;
                var isSuccess = _repo.Create(leaveType);       // Method of the ILeaveTypeRepository - remember it also calls Save() which returns a boolean
                if (!isSuccess)
                {
                    ModelState.AddModelError("", "Something went wrong...");
                    return(View(data));
                }

                return(RedirectToAction(nameof(Index)));
            }
            catch
            {
                ModelState.AddModelError("", "Something went wrong...");
                return(View());
            }
        }
        // GET: LeaveAllocationController
        public ActionResult Index(int count = 0)
        {
            var leaveTypes       = _leaveTypeRepo.FindAll();
            var mappedLeaveTypes = _mapper.Map <List <LeaveType>, List <LeaveTypeViewModel> >(leaveTypes.ToList());

            var model = new CreateLeaveTypeViewModel
            {
                LeaveTypes    = mappedLeaveTypes,
                NumberUpdated = count
            };

            return(View(model));
        }