locked
How to check if a record exists RRS feed

  • Question

  • User232486082 posted

    Greetings to all.
    I have my student registration form they have as properties ID,Name, Lastname. I want to check if a record (by ID) exists before storing it in database.

    public class Stundent
    {
        public int Id { get; set; }
        public string Name  { get; set; }
        public string LastName { get; set; }
    }
    
    public class StundentViewModel
    {
        public int Id { get; set; }
        public string Name  { get; set; }
        public string LastName { get; set; }
    }
    
    [HttpPost]
    public IActionResult Register(StundentViewModel vm)
    {
        var MyFirstQuery = Context.Stundent
                            .Where(x => x.Id == vm.Id)
    
        if (ModelState.IsValid)
        {
            Stundent model = new Stundent()
            {
                Id = vm.Id,
                Name = vm.Name,
                LastName = vm.LastName,
            };
    
            Context.Add(model);
            Context.SaveChanges();
        }
    }

    I am trying to make a query to do this task but I do not know how to do it

        var MyFirstQuery = Context.Stundent
                            .Where(x => x.Id == vm.Id)

    Saturday, June 8, 2019 11:34 PM

Answers

  • User533502567 posted

    var MyFirstQuery = Context.Stundent
                            .Where(x => x.Id == vm.Id)

    if (!Context.Stundent.Any(u => u.Id == vm.Id)) 
    {
    //record not exists 
    
    }



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, June 9, 2019 1:45 AM

All replies

  • User533502567 posted

    var MyFirstQuery = Context.Stundent
                            .Where(x => x.Id == vm.Id)

    if (!Context.Stundent.Any(u => u.Id == vm.Id)) 
    {
    //record not exists 
    
    }



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, June 9, 2019 1:45 AM
  • User232486082 posted

    Thank you!

    Tuesday, June 11, 2019 12:29 PM
  • User1520731567 posted

    Hi Alluka,

    As @budugu,said,you could use Any() in post action to check if a record exists,it is a simple demo.

    In the other way,if you would like check in front end,you could use Remote Validation,

    ing MVC 5. Remote Validation is mainly used to check if the user or Mail ID exists or not in a database. 

    Remote Validation permits the developer to call the controller action, using client side script. This is valuable when you need to perform out a back end query without performing a full Server side Postback.

    For example:

            [Remote("IsStudentExists", "Home", ErrorMessage = "The student already exists", HttpMethod = "post")]
            public int Id { get; set; }

    then call in IsStudentExists action in view by ajax to controller:

    public class HomeController: Controller {  
    .... [HttpPost] public JsonResult IsStudentExists(int Id) { var list = db.Student.ToList(); //check if any of the Id matches the Id specified in the Parameter using the ANY extension method. return Json((!list.Any(x => x.Id == Id) == false ? "false" : "true"), JsonRequestBehavior.AllowGet); }
    ... }

    More details,you could refer to this article:

    https://www.c-sharpcorner.com/article/remote-validation-in-mvc-5-to-check-if-username-and-email-id-exists/

    Best Regards.

    Yuki Tao

    Wednesday, June 12, 2019 1:59 AM