Id not accepting value Asp mvc 5

Asked

Viewed 31 times

0

People I am trying to do a login logic but in a way without using the other technologies of Asp mvc 5

[HttpPost]


[ValidateAntiForgeryToken]

public ActionResult Cadastro(Fornecedor fornecedo, int? id )

{

    var acesso = db.Fornecedor.Any(m => m.Codigo == id);

    if (acesso == false)

    {

        db.Fornecedor.Add(fornecedo);

        db.SaveChanges();

        return RedirectToAction("a");



    }

    if(acesso == true){
                ViewData["erro"] = "Mensagem de Erro";
                return View();
            }
            return View("z");
        }

When I register with same code it gives an error that already exists this code but when I see if it has any data from id to null.

  • Robson, the id is allowing null due to the int?, I don’t think I should allow it. What is happening is that your PK id accepts null, but a registration with null value has already been made and because it is PK it will not accept another. Your id is always going null, but with only what posted not to know why.

  • It seems that a piece of the end of the question is missing.. But when do I see if you have any id data for null... ? And I don’t understand: you are implementing a login logic without other technologies in a vendor register?

  • Actually I managed to find the error,in the code I am comparing the ID with m.Code , however I should buy the provided (my table) with the m.Code ,solution was thus var access = db.Fornecedor.Any(m => m.Code != provided.Code);

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.