-2

How can I check if some value already exist in database I am doing MVC with Entity Framework and I want to check if element with composite key already exist in database application does someone has any suggestion i GOT the Json object but my done() method doesn't work?

I tried with JsonResult method from my controller

public JsonResult Check(int? id1, int? id2)
    {
        IQueryable<InspekcijskaKontrola> listaKontrola = db.InspekcijskeKontrole.Include(i => i.InspekcijskaTijela).Include(i => i.Proizvod).Select(i => i);

        InspekcijskaKontrola inKontrola = listaKontrola.Where(i => i.InspekcijskoTijeloId == id1).Where(i => i.ProizvodId == id2).Select(i => i).Single();
        if (inKontrola!=null)
        {
            return Json(inKontrola, JsonRequestBehavior.AllowGet);
        }
        return Json(new InspekcijskaKontrola { InspekcijskoTijeloId = -1, ProizvodId = -1 }, JsonRequestBehavior.AllowGet);
    }

And I tried to rised modal dialog from my view with script

function prikazi() {
    var zahtjev = $.getJSON("/InspekcijskeKontrole/Check?id1=" + $("#kombo3").val() + "&id2=" + $("#kombo4").val());

    zahtjev.done(function (kontrola) {

        if (kontrola.InspekcijskoTijeloId != -1 && kontrola.ProizvodId != -1) {
            $("#p1").text("Inspekcijska kontrola za " + kontrola.ProizvodId + " je vec izvrsena");
            $("#modalni1").modal({ backdrop: "static" });
        }
    });
}
Jhony2
  • 3
  • 2
  • 6
  • Provide some code what you've already tried here. Some ways to check existing values in DB are through SQL query or LINQ with lambda expressions. – Tetsuya Yamamoto Jul 29 '16 at 08:43
  • It depends on what you use to communicate with the database. Are you using Entity Framework? – Kees Jul 29 '16 at 08:50
  • Yes I am using EF and I have composite key and I want to check if value with that key already exist in database if is so to pop up some message – Jhony2 Jul 29 '16 at 08:53
  • Then it looks like the place to go for you is an Entity Framework tutorial, not Stack Overflow. Read [ask] and share your research. – CodeCaster Jul 29 '16 at 08:56
  • See for example [Entity framework: How to return a row from a table with composite keys?](http://stackoverflow.com/questions/14781777/entity-framework-how-to-return-a-row-from-a-table-with-composite-keys) and [Best way to check if object exists in Entity Framework?](http://stackoverflow.com/questions/1802286/best-way-to-check-if-object-exists-in-entity-framework). – CodeCaster Jul 29 '16 at 09:32
  • I tried JsonResult method to get object with that composite key and from java script I tried to call that method and if object exist to rise modal dialog with informations, Can i do it in create ActionResult now when I check this JsonResult method I got circular reference excpetion? – Jhony2 Jul 29 '16 at 10:12
  • I got the object with `db.InspekcijskeKontroles.Find(int id1, int id2);` – Jhony2 Jul 29 '16 at 10:57

1 Answers1

-1

Providing a bit of code would really help. However, right off the bat I could suggest you use something like this to search for a key(or more) from an object:

if(dbContext.Items.Any(anyObjectName=>anyObjectName.firstKey == ValueYouLookFor 
                                   && anyObjectName.secondkey == AnotherValue))
{
    //logic to apply if object exists
}
Bogdan Ghiran
  • 38
  • 1
  • 6
  • better solution is Find(id1,id2) on dbSet – Jhony2 Jul 29 '16 at 11:01
  • Indeed, if the values are all part of the key, find works, but if you look for something like a user with an ID and his name, where the only key is the ID, it wouldn't work, you'd have to use either the Any(to check if it exists) method, or the First/FirstOrDefault(to get the object if it exists). Anyway, glad you found your solution, and good luck in the future – Bogdan Ghiran Jul 29 '16 at 11:38