示例#1
0
        public async Task <IActionResult> Create(InfoViewModel model)
        {
            if (ModelState.IsValid)
            {
                //validate if branc code exist
                if (!await Task.Run(() => context.Exist <tblBranch>((x) => x.Id == model.Branch_FK)))
                {
                    ViewBag.ErrorMessage = $"Brach with Id = {model.Branch_FK} cannot be found";
                    return(View("NotFound"));
                }

                //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"));
                }

                //instatiate new instance of tblInfo
                tblInfo table = new tblInfo()
                {
                    Branch_FK   = model.Branch_FK,
                    Certificate = model.Certificate,
                    Created     = DateTime.Now,
                    Id          = Guid.NewGuid().ToString(),
                    User_FK     = model.User_FK
                };

                await context.AddAsync <tblInfo>(table);

                return(RedirectToAction("index"));
            }

            //get all branch codes
            ViewBag.Branches = await context.GetAsync <tblBranch>();

            return(View(model));
        }