예제 #1
0
        public async Task <IActionResult> Upsert(int?id)
        {
            string token = HttpContext.Session.GetString("JWToken");
            IEnumerable <University> listUni = await _dbUNI.GetAllAsync(_urlUNI, token);

            PathWayVM pwVM = new PathWayVM()
            {
                UniversityList = listUni.Select(c => new SelectListItem
                {
                    Text  = c.Name,
                    Value = c.Id.ToString()
                }),
                PathWay = new PathWay()  //Has Id = 0 to check in Index View
            };

            // Insert
            if (id == null)
            {
                return(View(pwVM));
            }
            // Update
            pwVM.PathWay = await _dbPW.GetAsync(_urlPW, id.GetValueOrDefault(), token);

            if (pwVM.PathWay == null)
            {
                return(NotFound());
            }
            return(View(pwVM));
        }
예제 #2
0
        public async Task <IActionResult> Upsert(PathWayVM obj)
        {
            string token = HttpContext.Session.GetString("JWToken");

            if (ModelState.IsValid)
            {
                //Create
                if (obj.PathWay.Id == 0)
                {
                    await _dbPW.CreateAsync(_urlPW, obj.PathWay, token);
                }
                //Update
                else
                {
                    await _dbPW.UpdateAsync(_urlPW, obj.PathWay.Id, obj.PathWay, token);
                }
                TempData["Alert"] = "Modify Successfully !";
                return(RedirectToAction(nameof(Index)));
            }
            else
            {
                // Fix the validation not working when Create with 0 information added
                IEnumerable <University> listUni = await _dbUNI.GetAllAsync(_urlUNI, token);

                PathWayVM pwVM = new PathWayVM()
                {
                    UniversityList = listUni.Select(c => new SelectListItem
                    {
                        Text  = c.Name,
                        Value = c.Id.ToString()
                    }),
                    PathWay = obj.PathWay
                };
                return(View(pwVM));
            }
        }