Exemple #1
0
        public async Task <IActionResult> Update(TestTable model)
        {
            if (ModelState.IsValid)
            {
                //using (_context) <- this is dependency injection
                using (var context = new TestTableContext())
                {
                    TestTable user = new TestTable
                    {
                        Name = model.Name,
                        Id   = model.Id
                    };

                    context.TestTable.Update(user);
                    context.SaveChanges();
                    await context.SaveChangesAsync();

                    return(RedirectToAction("Index", "Home"));
                }
            }
            else
            {
                return(RedirectToAction("Error", "Home"));
            }
        }
Exemple #2
0
        public async Task <IActionResult> Add(TestTable model)
        {
            if (ModelState.IsValid)
            {
                //using (_context) <- this is dependency injection
                using (var context = new TestTableContext())
                {
                    TestTable user = new TestTable
                    {
                        Name = model.Name,
                        Id   = model.Id
                    };

                    //Only use AddSync So if you use a value generator that might need to access the DB to get new values to assign to new entries, such as the SequenceHiLo generator, then use AddAsync().
                    context.TestTable.Add(user);

                    await context.SaveChangesAsync();

                    return(RedirectToAction("Index", "Home"));
                }
            }
            else
            {
                return(RedirectToAction("Error", "Home"));
            }
        }
        public async Task <ActionResult <TestTable> > PostTestTable([FromBody] TestTable item)
        {
            try
            {
                _context.TestTable.Add(item);
                await _context.SaveChangesAsync();

                /*
                 * return StatusCode(200);
                 */
                //return http 200
                return(Ok());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return(StatusCode(500));
            }



            //return http 201 created
            //return CreatedAtAction(nameof(Get), new { id = item.Id, name=item.Name}, item);
        }