public async Task<ActionResult> Create(SchoolClassViewModel schoolclass)
        {
            if (ModelState.IsValid)
            {
                
                SchoolClass scObj = Mapper.Map<SchoolClass>(schoolclass);
                using(var dbtransaction = db.Database.BeginTransaction())
                {
                    try
                    {
                        
                        var LastClassOrderNo = db.Database.SqlQuery<int>("Select ISNULL(Max(ClassOrder),0) from SchoolClasses with (XLOCK,HOLDLOCK) where SchoolRefID={0}", scObj.SchoolRefID).SingleOrDefault<int>();
                        scObj.ClassOrder = LastClassOrderNo + 1;
                        db.SchoolClasses.Add(scObj);
                        await db.SaveChangesAsync();
                        dbtransaction.Commit();
                        return RedirectToAction("Index");
                        
                    }
                    catch(Exception e)
                    {
                        dbtransaction.Rollback();
                        TempData["errormessage"] = e.Message;
                        ViewBag.SchoolRefID = new SelectList(db.Schools.AsNoTracking().Select(x => new { x.SchoolID, x.SchoolName }), "SchoolID", "SchoolName", schoolclass.SchoolRefID);
                        return View(schoolclass);

                    }
                }
                
                
            }

            ViewBag.SchoolRefID = new SelectList(db.Schools.AsNoTracking().Select(x => new { x.SchoolID,x.SchoolName}), "SchoolID", "SchoolName", schoolclass.SchoolRefID);
            return View(schoolclass);
        }
        public bool addNewClasstoSchool(SchoolClassViewModel SchoolVM)
        {
            SchoolClass schoolclassmodel = Mapper.Map<SchoolClass>(SchoolVM);
            using (var dbtransaction = db.Database.BeginTransaction())
            {
                try
                {

                    var LastClassOrderNo = db.Database.SqlQuery<int>("Select ISNULL(Max(ClassOrder),0) from SchoolClasses with (XLOCK,HOLDLOCK) where SchoolRefID={0}", schoolclassmodel.SchoolRefID).SingleOrDefault<int>();
                    schoolclassmodel.ClassOrder = LastClassOrderNo + 1;
                    db.SchoolClasses.Add(schoolclassmodel);
                    db.SaveChanges();
                    dbtransaction.Commit();
                    return true;

                }
                catch (Exception e)
                {
                    dbtransaction.Rollback();
                    _modelState.AddModelError("", "The class was not able to add to school.Please try again later" + e.Message);
                    return false;
                }
            }
        }
 public bool saveClassAfterEditing(SchoolClassViewModel SchoolClassVM)
 {
     SchoolClass SchoolClassModel = Mapper.Map<SchoolClass>(SchoolClassVM);
     db.Entry(SchoolClassModel).State = EntityState.Modified;
     db.SaveChanges();
     return true;
 }
        public async Task<ActionResult> Edit(SchoolClassViewModel schoolclass)
        {
            if (ModelState.IsValid)
            {

                SchoolClass changetoschoolclass = Mapper.Map<SchoolClass>(schoolclass);
                db.Entry(changetoschoolclass).State = EntityState.Modified;
                await db.SaveChangesAsync();
                return RedirectToAction("Index");
            }
            ViewBag.SchoolRefID = new SelectList(db.Schools.AsNoTracking().Select(x => new { x.SchoolID,x.SchoolName}), "SchoolID", "SchoolName", schoolclass.SchoolRefID);
            return View(schoolclass);
        }