public JsonResult DeleteSubject(EPortal.Models.Subject Subject)
        {
            int    result = 0;
            string orgid  = User.OrgId;

            string errormsg = string.Empty;

            using (EPortalEntities entity = new EPortalEntities())
            {
                //var checkref = (from r in entity.Subjects
                //                where r.OrganizationID == orgid
                //                && r.Id == Subject.Id
                //                select r).FirstOrDefault();
                //if (checkref != null)
                //{
                //    errormsg = "Operation conflict:Operation cannot be performed.Record already in Used.";
                //}
                //else
                //{

                //Subject subjectinfo = (from sub in entity.Subjects
                //                       where sub.Id == Subject.Id
                //                       && sub.OrganizationID == orgid
                //                       select sub).FirstOrDefault();
                entity.Entry(Subject).State = System.Data.Entity.EntityState.Deleted;


                //List<SubjectDetail> subjectdetails = (from detsub in entity.SubjectDetails
                //                                      where detsub.SubjectId == Subject.Id
                //                                      && detsub.OrganizationID == orgid
                //                                      select detsub).ToList();
                //if (subjectdetails.Count() > 0)
                //{
                //    foreach (SubjectDetail item in subjectdetails)
                //    {
                //        entity.Entry(item).State = System.Data.Entity.EntityState.Deleted;
                //    }

                //}

                result = entity.SaveChanges();
                //}
            }

            return(Json(new { result = result > 0 ? true : false, errormsg = errormsg }, JsonRequestBehavior.AllowGet));
        }
        public JsonResult GetSubjectInfo(EPortal.Models.Subject Subject)
        {
            //string orgid = Session["OrgId"].ToString();

            string orgid = User.OrgId;

            SubjectList roleinforole = new SubjectList();
            //List<SubjectDetail> subjectdetails = new List<SubjectDetail>();
            List <Courselist> courselist = new List <Courselist>();

            using (EPortalEntities entity = new EPortalEntities())
            {
                roleinforole = (from o in entity.Subjects
                                where o.Id == Subject.Id &&
                                o.OrganizationID == orgid
                                select new SubjectList
                {
                    Id = o.Id,
                    Code = o.Code,
                    Name = o.Name,
                    Operation = "Edit",
                }).FirstOrDefault();



                courselist = GetCourseListinfo();
                //var subjectlist = (from subjd in entity.SubjectDetails
                //                   where subjd.OrganizationID == orgid
                //                   && subjd.SubjectId == roleinforole.Id
                //                   select subjd).ToList();
                //foreach (Courselist item in courselist)
                //{
                //    var checkselectedornot = subjectlist.Where(x => x.CourseId.Contains(item.Id)).FirstOrDefault();
                //    if (checkselectedornot != null)
                //    {

                //        item.Selected = true;
                //    }


                //}
            }
            return(Json(new { subjectdata = roleinforole, coursedata = courselist }, JsonRequestBehavior.AllowGet));
        }
        public JsonResult SaveSubject(EPortal.Models.Subject Subjectinfo, List <Courselist> Courseinfo)
        {
            string errormsg = "";
            int    result   = 0;

            //if ((role.Code != "" || role.Code != null) && (role.Name != "" || role.Name != null))
            {
                //string orgid = Session["OrgId"].ToString();

                string orgid = User.OrgId;


                using (EPortalEntities entity = new EPortalEntities())
                {
                    if (Subjectinfo.Operation == "Create")
                    {
                        var checkrolecode = (from r in entity.Subjects
                                             where r.OrganizationID == orgid &&
                                             r.Code == Subjectinfo.Code
                                             select r).FirstOrDefault();
                        if (checkrolecode == null)
                        {
                            #region For Course but comented
                            //List<Courselist> seelctedcourse = Courseinfo.Where(x => x.Selected == true).ToList();

                            //if (seelctedcourse.Count() > 0)
                            //{


                            //    Subjectinfo.Id = Guid.NewGuid().ToString();
                            //    Subjectinfo.OrganizationID = orgid;
                            //    Subjectinfo.RowState = true;
                            //    Subjectinfo.CreateDateTime = System.DateTime.Now;
                            //    entity.Entry(Subjectinfo).State = System.Data.Entity.EntityState.Added;
                            //    entity.Subjects.Add(Subjectinfo);


                            //    foreach (Courselist item in seelctedcourse)
                            //    {
                            //        sibjectdetailinfo = new SubjectDetail();
                            //        sibjectdetailinfo.Id = Guid.NewGuid().ToString();
                            //        sibjectdetailinfo.SubjectId = Subjectinfo.Id;
                            //        sibjectdetailinfo.CourseId = item.Id;
                            //        sibjectdetailinfo.OrganizationID = orgid;
                            //        sibjectdetailinfo.RowState = true;
                            //        sibjectdetailinfo.CreateDateTime = System.DateTime.Now;
                            //        entity.Entry(sibjectdetailinfo).State = System.Data.Entity.EntityState.Added;
                            //        entity.SubjectDetails.Add(sibjectdetailinfo);
                            //    }

                            //}
                            #endregion


                            try
                            {
                                Subjectinfo.Id             = Guid.NewGuid().ToString();
                                Subjectinfo.OrganizationID = User.OrgId;
                                Subjectinfo.CreateDateTime = DateTime.Now;
                                Subjectinfo.RowState       = true;


                                entity.Entry(Subjectinfo).State = System.Data.Entity.EntityState.Added;
                                result = entity.SaveChanges();
                            }
                            catch (Exception)
                            {
                            }
                        }

                        else
                        {
                            errormsg = "Subject already exist with same Code.";
                        }
                    }
                    else
                    {
                        EPortal.Models.Subject roledata = (from o in entity.Subjects
                                                           where o.OrganizationID == orgid &&
                                                           o.Id == Subjectinfo.Id
                                                           select o
                                                           ).FirstOrDefault();

                        roledata.Code = Subjectinfo.Code;
                        roledata.Name = Subjectinfo.Name;
                        entity.Entry(roledata).State = System.Data.Entity.EntityState.Modified;


                        #region Couse BUt Comented
                        //foreach (Courselist item in Courseinfo)
                        //{
                        //    SubjectDetail subjectdetails = (from subjd in entity.SubjectDetails
                        //                                    where subjd.OrganizationID == orgid
                        //                                    && subjd.SubjectId == roledata.Id
                        //                                    && subjd.CourseId == item.Id
                        //                                    select subjd).FirstOrDefault();
                        //    if (subjectdetails != null)
                        //    {
                        //        if (item.Selected == false)
                        //        {
                        //            entity.Entry(subjectdetails).State = System.Data.Entity.EntityState.Deleted;

                        //        }
                        //        else
                        //        {
                        //            entity.Entry(subjectdetails).State = System.Data.Entity.EntityState.Modified;

                        //        }
                        //    }
                        //    else
                        //    {
                        //        if (item.Selected == true)
                        //        {
                        //            sibjectdetailinfo = new SubjectDetail();
                        //            sibjectdetailinfo.Id = Guid.NewGuid().ToString();
                        //            sibjectdetailinfo.SubjectId = roledata.Id;
                        //            sibjectdetailinfo.CourseId = item.Id;
                        //            sibjectdetailinfo.OrganizationID = orgid;
                        //            sibjectdetailinfo.RowState = true;
                        //            sibjectdetailinfo.CreateDateTime = DateTime.Now;
                        //            entity.Entry(sibjectdetailinfo).State = System.Data.Entity.EntityState.Added;

                        //        }

                        //    }

                        //}
                        #endregion



                        try
                        {
                            result = entity.SaveChanges();
                        }
                        catch (Exception ex)
                        {
                        }
                    }
                }
            }
            return(Json(new { result = result > 0 ? true : false, errormsg = errormsg }, JsonRequestBehavior.AllowGet));
        }