Beispiel #1
0
        //Add  & Edit COmmittee Member
        
        public async Task<ActionResult> CreateCommitteeMember(AffiliationCommitteeMember model)
        {
            if (!ModelState.IsValid)
            {
                var Asdesignation = db.Affi_com_Designation.ToList();
                model.AsMember = Asdesignation;
                TempData["ModelState"] = "Check The Entered Information";
                if (model.UserId == null)
                    return View("AffiliationCommittee", model);
                else if (model.UserId != null)
                    return View("EditCommitteeMember", model);
            }
            try
            {
                //var UserStore = new UserStore<ApplicationUser>(new ApplicationDbContext());
                //var UserManager = new UserManager<ApplicationUser>(UserStore);
                if (model.UserId == null)
                {
                    var User = new ApplicationUser()
                    {
                        UserName = model.UserName,
                        UserType = 11,
                        isDisable = false,
                        PhoneNumber=model.ACMember.Cell,
                        Email = model.Email
                    };
                    var result = await UserManager.CreateAsync(User, model.Password);
                    if (result.Succeeded)
                    {
                        await UserManager.AddToRoleAsync(User.Id, "Member");
                        //TempData["CreateCommitteeMember"] = model;
                        //TempData["ReturnUrl"] = "/AffiliationAdminSide/CreateCommitteeMemberAfterTwoFactorRegister";
                        //return RedirectToAction("VerifyCellNo", "Accounts", new { PhoneNumber = model.ACMember.Cell, UserId = User.Id });
                        var asdesignation = db.BI_affi_pop_affi_com_designation();
                        foreach (var member in asdesignation)
                        {
                            if (model.ACMember.com_designation_ID == member.ID)
                            {
                                model.ACMember.As_Com_Designation = member.Designation;
                            }
                        }
                        if (model.ACMember.ID == null)
                        {
                            ObjectParameter output = new ObjectParameter("UserId", typeof(Int32));
                            db.TA3_affi_Create_affiliation_com(model.ACMember.Name, model.ACMember.Designation, model.ACMember.As_Com_Designation, model.ACMember.Department, model.ACMember.CNIC, model.ACMember.Cell, model.ACMember.for_the_year, model.ACMember.com_designation_ID, output);
                            db.SaveChanges();
                            User.Institute_ID = Convert.ToInt32(output.Value);
                            var updateresult = await UserManager.UpdateAsync(User);
                            if (updateresult.Succeeded)
                            {
                                TempData["CreatedSuccessfully"] = "Committee Member Created Successfully";

                            }
                        }
                    }//result.succeeded if ended here
                    else
                    {
                        var errorstring = "";
                        foreach (var error in result.Errors)
                            errorstring = errorstring + error + " ";
                        TempData["UserCreateError"] = errorstring;
                        return RedirectToAction("AffiliationCommittee","AffiliationAdminSide");
                    }
                }//if ending

                else
                {
                    var dbuser = await UserManager.FindByIdAsync(model.UserId);
                    if (dbuser != null)
                    {
                        var newPasswordHash = UserManager.PasswordHasher.HashPassword(model.Password);
                        dbuser.UserName = model.UserName;
                        dbuser.PasswordHash = newPasswordHash;
                        var result = UserManager.Update(dbuser);
                        if (result.Succeeded)
                        {
                            db.BI_affi_update_affiliation_com(model.ACMember.ID, model.ACMember.Name, model.ACMember.Designation, model.ACMember.As_Com_Designation, model.ACMember.Department, model.ACMember.CNIC, model.ACMember.Cell, model.ACMember.com_designation_ID);
                            db.SaveChanges();
                            TempData["UpdatedSuccessfully"] = "User Updated Successfully";
                            return RedirectToAction("/AffiliationCommittee");
                        }
                    }
                    else
                    {
                        TempData["UserNotExist"] = "User Cannot Be Updated";
                        return View("AffiliationCommittee", model);
                    }
                }
                return RedirectToAction("/AffiliationCommittee");
            }//try ending 
            catch (Exception e)
            {
                TempData["Exception"] = "There Is Error In Updating Or Creating Contact Developer If It Shows Again";
                return RedirectToAction("/AffiliationCommittee");
            }
        }