//public async Task<IActionResult> CreateUpdateDB([Bind("CrewId,Name,MobNo,Email,Address,DateOfBirth,Type,Status")] Crew crew)
        public IActionResult CreateRecordConfirmed(Crew FullAddress)
        {
            //Convert TempData values back into respective types
            FullAddress.Name        = Convert.ToString(TempData["Name"]);
            FullAddress.DateOfBirth = Convert.ToDateTime(TempData["DateOfBirth"]);
            FullAddress.Address     = Convert.ToString(TempData["Address"]);
            FullAddress.MobNo       = Convert.ToString(TempData["MobNo"]);
            FullAddress.Email       = Convert.ToString(TempData["Email"]);
            //FullAddress.CrewId = Convert.ToInt32(TempData["CrewId"]);
            FullAddress.Type = Convert.ToString(TempData["Type"]);

            //Code below writes form values to SQL DB if ModelState valid
            //**Removed ModelState validation as passing input view to a review view before submission so data has to be transferred back
            //**to controller via Viewdata object, not input form therefore ModelState is false by default.

            //var errors = ModelState.Values.SelectMany(v => v.Errors);
            //if (ModelState.IsValid)
            {
                //Save changes to DB
                _context.Add(FullAddress);
                //Added SQL commands below to allow custom PK values i.e. CrewId (Employee ID)
                //Didn't work as using EF Core 2, tried workaround https://docs.microsoft.com/en-us/ef/core/saving/explicit-values-generated-properties which doesn't work on PK values...losing the will to live!
                //_context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT dbo.Crew ON");
                _context.SaveChanges();
                //_context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT dbo.Crew OFF");
                _context.Update(FullAddress);
                return(RedirectToAction(nameof(Index)));
            }
            //return RedirectToAction("Create", FullAddress);
        }
Пример #2
0
        public async Task <IActionResult> Edit(int id, [Bind("ID,CrewID,Name,DateOfBirth,Type")] Crew crew)
        {
            if (id != crew.ID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(crew);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!CrewExists(crew.ID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(crew));
        }