예제 #1
0
        public async Task <IActionResult> EditUnit(int id, [Bind("Id, UnitCode, UnitName, CourseId, UniversityId")] UnitDetailsModel unit)
        {
            if (id != unit.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _db.Update(unit); // update Book name
                    await _db.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!UnitExist(unit.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(UnitManagement))); // redirect to index
            }
            return(View(unit));
        }
예제 #2
0
        public async Task <UnitDetailsModel> Get(int unitId)
        {
            var model = new UnitDetailsModel();

            try
            {
                model = await _context.GetUnit(unitId);
            }
            catch (Exception ex)
            {
                if (model == null)
                {
                    model = new UnitDetailsModel();
                }

                if (model.Message.Length > 0)
                {
                    model.Message += " | ";
                }
                else
                {
                    model.Message = "Error: ";
                }

                model.Message += ex.Message;
            }

            return(model);
        }
예제 #3
0
        public async Task <UnitDetailsModel> GetUnitByInventoryId(int inventoryId)
        {
            UnitDetailsModel model = new UnitDetailsModel();

            try
            {
                model = await GetUnitFromDB(null, inventoryId);
            }
            catch (Exception ex)
            {
                model.Message = $"Error: {ex.Message}";
            }

            return(model);
        }
예제 #4
0
        public async Task <UnitDetailsModel> GetUnit(int unitId)
        {
            var model = new UnitDetailsModel();

            try
            {
                model = await GetUnitFromDB(unitId, null);
            }
            catch (Exception ex)
            {
                model.Message = $"Error: {ex.Message}";
            }

            return(model);
        }
예제 #5
0
        [HttpPost] //post method
        public async Task <IActionResult> AddUnit([Bind("Id, UnitCode, UnitName, CourseId, UniversityId ")] UnitDetailsViewModel unit)
        {
            if (ModelState.IsValid)
            {
                var newUnit = new UnitDetailsModel()
                {
                    Id         = unit.Id,
                    UnitName   = unit.UnitName,
                    UnitCode   = unit.UnitCode,
                    Course     = _db.Course.Find(unit.UniversityId),
                    University = _db.University.Find(unit.UniversityId)
                };

                _db.Add(newUnit);                                 //add data to University table
                await _db.SaveChangesAsync();                     //wait for database response

                return(RedirectToAction(nameof(UnitManagement))); // redirect to index
            }

            return(View(unit));
        }