Ejemplo n.º 1
0
        public async Task <IActionResult> Branch()
        {
            var user = await userManager.GetUserAsync(User);

            BrancViewModel model = new BrancViewModel()
            {
                Username = user?.Email,
                User_FK  = user?.Id
            };

            return(View(model));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Branch(BrancViewModel model)
        {
            if (ModelState.IsValid)
            {
                //check if the user exist
                var user = await userManager.FindByIdAsync(model.User_FK);

                if (user == null)
                {
                    ViewBag.ErrorMessage = $"User with Id = {model.User_FK} cannot be found";
                    return(View("NotFound"));
                }

                //Check if the brach has the same brach code
                if (context.Exist <tblBranch>((x) => x.Code == model.Code))
                {
                    ModelState.AddModelError("", $"Brach code: {model.Code} already exist");
                    return(View(model));
                }

                tblBranch table = new tblBranch()
                {
                    Code    = model.Code,
                    Created = DateTime.Now,
                    Id      = Guid.NewGuid().ToString(),
                    Name    = model.Branch,
                    User_FK = model.User_FK
                };

                await context.AddAsync <tblBranch>(table);

                return(RedirectToAction("branches"));
            }

            return(View(model));
        }