public int InsertSub(SubTranModel model)
 {
     try
     {
         Mapper.CreateMap <SubTranModel, SubTran>();
         SubTran objUser = Mapper.Map <SubTran>(model);
         Dbcontext.SubTrans.Add(objUser);
         return(Dbcontext.SaveChanges());
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
Exemplo n.º 2
0
        public ActionResult Index(StudentModel model, SubTranModel model1, string cmd)
        {
            StudentService     objService = new StudentService();
            HttpPostedFileBase logo       = Request.Files["fileLogo"];
            HttpPostedFileBase sign       = Request.Files["fileSign"];
            HttpPostedFileBase thumb      = Request.Files["fileThumb"];
            int uid  = 0;
            int acid = 0;
            int did  = 0;

            if (Session["UID"] != null)
            {
                uid  = Convert.ToInt32(Session["UID"].ToString());
                acid = Convert.ToInt32(Session["ACID"].ToString());
                did  = Convert.ToInt32(Session["DID"].ToString());
            }
            model.DID = did;

            if (logo != null)
            {
                var logoPhoto = Path.GetFileName(logo.FileName);

                if (logoPhoto != "")
                {
                    var path = Path.Combine(Server.MapPath("/PhotoUpload/Photo"), logoPhoto);
                    logo.SaveAs(path);
                    model.Photo = logoPhoto;
                }
            }
            if (sign != null)
            {
                var signFile = Path.GetFileName(sign.FileName);
                if (signFile != "")
                {
                    var path = Path.Combine(Server.MapPath("/PhotoUpload/Sign"), signFile);
                    sign.SaveAs(path);
                    model.Sign = signFile;
                }
            }
            if (thumb != null)
            {
                var thumbPhoto = Path.GetFileName(thumb.FileName);

                if (thumbPhoto != "")
                {
                    var path = Path.Combine(Server.MapPath("/PhotoUpload/Thumb"), thumbPhoto);
                    thumb.SaveAs(path);
                    model.Thumb = thumbPhoto;
                }
            }
            if (cmd == "Save")
            {
                try
                {
                    var    lstDist = Dbcontext.DistrictMasters.Where(m => m.DID == model.DID).SingleOrDefault();
                    string name    = lstDist.Name.Substring(0, 3);
                    var    prefix  = Thread.CurrentThread.CurrentCulture.TextInfo.ToUpper(name);
                    model.Prefix      = prefix + "/";
                    model.Status      = true;
                    model.CreatedBy   = uid;
                    model.Verified    = false;
                    model.CreatedDate = System.DateTime.Now;
                    model.ACID        = acid;
                    int sid = objService.Insert(model);

                    model1.StudID = sid;

                    foreach (var s in model.ListSub)
                    {
                        model1.SubID     = s.SubID;
                        model1.IsChecked = s.IsChecked;
                        objService.InsertSub(model1);
                    }
                    TempData["AMsg"] = "Saved Successfully!";
                }
                catch (Exception ex)
                {
                    TempData["AMsg"] = "Error Occured, " + ex;
                }
            }
            else
            {
                try
                {
                    model.UpdatedBy   = uid;
                    model.UpdatedDate = System.DateTime.Now;
                    objService.Update(model);

                    foreach (var s in model.ListSub)
                    {
                        SubTran sub = Dbcontext.SubTrans.Where(u => u.StudID == model.StudID && u.SubID == s.SubID).SingleOrDefault();
                        if (sub != null)
                        {
                            sub.StudID    = model.StudID;
                            sub.SubID     = s.SubID;
                            sub.IsChecked = s.IsChecked;
                            Dbcontext.SaveChanges();
                        }
                        else
                        {
                            model1.StudID    = model.StudID;
                            model1.SubID     = s.SubID;
                            model1.IsChecked = s.IsChecked;
                            objService.InsertSub(model1);
                        }
                    }

                    TempData["AMsg"] = "Updated Successfully!";
                }
                catch (Exception ex)
                {
                    TempData["AMsg"] = "Error Occured," + ex;
                }
            }


            return(RedirectToAction("Index"));
        }