Exemplo n.º 1
0
 private void buttonAddNewDog_Click(object sender, EventArgs e)
 {
     try
     {
         MyDog dog = new MyDog();
         if (textBox1.Text != "")
         {
             dog.Nickname = textBox1.Text;
         }
         else
         {
             throw new Exception("Error! Empty field Nickname");
         }
         if (textBox2.Text != "")
         {
             dog.Height = int.Parse(textBox2.Text);
         }
         else
         {
             throw new Exception("Error! Wrong field height");
         }
         Breed breed = db.Breeds.FirstOrDefault(a => a.BreedName == comboBox2.SelectedItem.ToString());
         if (breed != null)
         {
             dog.BreedId = breed.Id;
         }
         db.Dogs.Add(dog);
         db.SaveChangesAsync();
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }
 }
Exemplo n.º 2
0
        public async Task <IActionResult> PutDog([FromRoute] string id, [FromBody] Dog dog)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != dog.ID)
            {
                return(BadRequest());
            }

            _context.Entry(dog).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!DogExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Exemplo n.º 3
0
        public async Task <IActionResult> Create([Bind("Id,Name,Age")] Owner owner)
        {
            if (ModelState.IsValid)
            {
                _context.Add(owner);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(owner));
        }
Exemplo n.º 4
0
 public async Task <IActionResult> AddNewDog([FromBody] Dog dog)
 {
     _context.Dogs.Add(dog);
     try
     {
         await _context.SaveChangesAsync();
     }
     catch (Exception ex)
     {
         return(new JsonResult(new AjaxResult(false, "Failed to add new dog.")));
     }
     return(new JsonResult(new EntityAddedAjaxResult(dog.Id, "New dog added successfully.")));
 }