1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

  2. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[SQL] Check for duplicates before adding to the database

Discussão em 'Outras Linguagens' iniciado por Stack, Julho 23, 2021.

  1. Stack

    Stack Membro Participativo

    I have a simple database with brand names. This is my model class:

    Brand.cs

    [Key]
    public int BrandID { get; set; }

    [Required(ErrorMessage = "Enter Brandname")]
    [Display(Name = "Brandname")]
    [StringLength(30)]
    public string BrandName { get; set; }


    The controller part to post an new brand name in the SQL database looks like this.

    Brandscontroller.cs

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Create([Bind("BrandID,BrandName")] Brand brand)
    {
    if (ModelState.IsValid)
    {
    _context.Add(brand);
    await _context.SaveChangesAsync();
    return RedirectToAction(nameof(Index));
    }

    return View(brand);
    }


    How can I prevent, or give a signal to the user, that a brand name already exists when he adds one that is already in the database?

    Continue reading...

Compartilhe esta Página