Exemplo n.º 1
0
        public JsonResult UpdateXepLop(string models)
        {
            try
            {
                var xepLops = JsonConvert.DeserializeObject <IEnumerable <XepLopViewModel> >(models);
                if (xepLops != null)
                {
                    List <HocSinh> listHocSinh = new List <HocSinh>();
                    foreach (XepLopViewModel xeplop in xepLops)
                    {
                        //Chua xep lop truoc do
                        HocSinh temp = _HocSinhService.GetByIDWithChild(xeplop.PersonId);
                        if (temp.dsLopHoc.Where(m => m.MaNamHoc == xeplop.MaNamHoc).Count() == 0)
                        {
                            temp.dsLopHoc.Add(_LopHocService.GetByID(xeplop.MaLopHoc));
                            _HocSinhService.Update(temp);
                        }
                        else
                        {
                            LopHoc lopHoc = temp.dsLopHoc.Where(m => m.MaNamHoc == xeplop.MaNamHoc).FirstOrDefault();
                            temp.dsLopHoc.Remove(lopHoc);
                            if (xeplop.MaLopHoc != -1)
                            {
                                temp.dsLopHoc.Add(_LopHocService.GetByID(xeplop.MaLopHoc));
                            }
                            _HocSinhService.Update(temp);
                        }
                    }

                    //cap nhat si so
                    List <int> idLopChanged = xepLops.Select(m => m.MaLopHoc).Distinct().ToList();
                    foreach (int idLop in idLopChanged)
                    {
                        LopHoc lop = _LopHocService.GetByIDWithChild(idLop);
                        lop.SiSo = lop.dsHocSinh.Count;
                        _LopHocService.Update(lop);
                    }
                }
                return(Json(xepLops));
            }
            catch (Exception e)
            {
                return(Json(new { error = e.Message }));
            }
        }
Exemplo n.º 2
0
 public JsonResult Update(string models)
 {
     try
     {
         var dsHocSinh = JsonConvert.DeserializeObject <IEnumerable <HocSinh> >(models);
         if (dsHocSinh != null)
         {
             _HocSinhService.Update(dsHocSinh);
         }
         return(Json(dsHocSinh));
     }
     catch (Exception ex)
     {
         //ShowMessage here
         return(Json(null));
     }
 }