public ActionResult Add(SIMCardInfoView model) { if (ModelState.IsValid) { SIMCard simCard = null; //Check if entity exists in database simCard = db.SIMCards.FirstOrDefault(card => card.ICCID == model.ICCID); if (model.IsNew) { // The posted model indicated this is a new simcard. simCard = new SIMCard(); db.SIMCards.Add(simCard); } else { // This model already exists // The entity could not be saved, add an error and return to the view. ModelState.AddModelError("Validation", "Unable to save this simcard. The database entity exists in the database."); UpdateDetailViewModel(model); return View(model); } //TODO:Default group //simCard.GroupID = 1; simCard.CapturedBy = "Test"; simCard.DateCaptured = DateTime.Now; simCard.ICCID = model.ICCID; simCard.IMSI = model.IMSI; simCard.MSISDN = model.MSISDN; simCard.NetworkID = model.NetworkID; simCard.PackageID = model.PackageID; simCard.SIMCardID = model.SIMCardID; simCard.SubBatchID = model.SubBatchID; simCard.TrackingCode = model.TrackingCode; db.SaveChanges(); return RedirectToAction("Index"); } else { // The user did not post a valid model. // To debug this, check the errors inside the modelstate object. UpdateDetailViewModel(model); return View(model); } }
bool hasData = db.SIMCards.Any(card => card.ICCID == model.ICCID); if (hasData ){ // entity exists in database } else { // nope, not exists in database }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)