public ActionResult Create(NewCRMLeadViewModel vm)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var newCRMLead = new CRMLead
                    {
                        AssignedToUserId = vm.AssignedToUserId,
                        LeadSourceId     = vm.LeadSourceId,
                        CategoryId       = vm.CategoryId,
                        LeadStatusId     = vm.LeadStatusId,
                        Person           = vm.Person,
                        Expertise        = vm.Expertise,
                        Description      = vm.Description,
                        RecievedOn       = vm.RecievedOn,
                        CreatedByUserId  = WebUser.Id
                    };

                    _crmLeadRepository.Create(newCRMLead);
                    _unitOfWork.Commit();

                    // Map the Technologies
                    if (vm.TechnologyIds != null)
                    {
                        foreach (var technologyId in vm.TechnologyIds)
                        {
                            var newMap = new CRMLeadTechnologyMap
                            {
                                LeadId       = newCRMLead.Id,
                                TechnologyId = technologyId
                            };

                            _crmLeadTechnologyMapRepository.Create(newMap);
                        }

                        _unitOfWork.Commit();
                    }

                    return(RedirectToAction("Index"));
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError(string.Empty, ex.Message);
                }
            }
            else
            {
                ModelState.AddModelError(string.Empty, "Please fill all mandatory fields");
            }

            return(View(vm));
        }
        public ActionResult Edit(EditCRMLeadViewModel vm)
        {
            if (ModelState.IsValid)
            {
                var selectedLead = _crmLeadRepository.GetBy(l => l.Id == vm.Id);

                if (selectedLead != null)
                {
                    // Check whether i have access to this Lead details
                    var hasAccess = selectedLead.AssignedToUserId == WebUser.Id || DoIHaveCRMManageAccess();
                    if (!hasAccess)
                    {
                        return(RedirectToAction("NotAuthorized", "Error", new { area = "" }));
                    }

                    selectedLead.AssignedToUserId = vm.AssignedToUserId;
                    selectedLead.LeadSourceId     = vm.LeadSourceId;
                    selectedLead.LeadSourceUserId = vm.LeadSourceUserId;
                    selectedLead.CategoryId       = vm.CategoryId;

                    selectedLead.Person.FirstName            = vm.Person.FirstName;
                    selectedLead.Person.LastName             = vm.Person.LastName;
                    selectedLead.Person.Gender               = vm.Person.Gender;
                    selectedLead.Person.Email                = vm.Person.Email;
                    selectedLead.Person.Organization         = vm.Person.Organization;
                    selectedLead.Person.Designation          = vm.Person.Designation;
                    selectedLead.Person.PhoneNo              = vm.Person.PhoneNo;
                    selectedLead.Person.SecondaryEmail       = vm.Person.SecondaryEmail;
                    selectedLead.Person.OfficePhone          = vm.Person.OfficePhone;
                    selectedLead.Person.Website              = vm.Person.Website;
                    selectedLead.Person.Skype                = vm.Person.Skype;
                    selectedLead.Person.Facebook             = vm.Person.Facebook;
                    selectedLead.Person.Twitter              = vm.Person.Twitter;
                    selectedLead.Person.GooglePlus           = vm.Person.GooglePlus;
                    selectedLead.Person.LinkedIn             = vm.Person.LinkedIn;
                    selectedLead.Person.City                 = vm.Person.City;
                    selectedLead.Person.Country              = vm.Person.Country;
                    selectedLead.Person.Address              = vm.Person.Address;
                    selectedLead.Person.CommunicationAddress = vm.Person.CommunicationAddress;
                    selectedLead.Person.DateOfBirth          = vm.Person.DateOfBirth;

                    selectedLead.Expertise   = vm.Expertise;
                    selectedLead.Description = vm.Description;
                    selectedLead.RecievedOn  = vm.RecievedOn;

                    selectedLead.UpdatedByUserId = WebUser.Id;

                    _crmLeadRepository.Update(selectedLead);
                    _unitOfWork.Commit();


                    // Remove the existing mapped Technologies
                    var existingMaps = _crmLeadTechnologyMapRepository.GetAllBy(m => m.LeadId == selectedLead.Id);
                    foreach (var map in existingMaps)
                    {
                        _crmLeadTechnologyMapRepository.Delete(map);
                    }

                    _unitOfWork.Commit();

                    if (vm.TechnologyIds != null)
                    {
                        // Map the New Technologies
                        foreach (var technologyId in vm.TechnologyIds)
                        {
                            var newMap = new CRMLeadTechnologyMap
                            {
                                LeadId       = vm.Id,
                                TechnologyId = technologyId
                            };

                            _crmLeadTechnologyMapRepository.Create(newMap);
                        }

                        _unitOfWork.Commit();
                    }

                    return(RedirectToAction("Index"));
                }
            }

            return(View(vm));
        }