public ActionResult Create(long Id = 0)
        {
            ViewBag.SuccessMessage = "";

            ContactFormModel contactFormModel = new ContactFormModel();

            if(Id != 0)
            {
                Contact contact = contactService.GetContact(Id);
                contactFormModel = AutoMapper.Mapper.Map<Contact, ContactFormModel>(contact);

                if(contact.LeadSource != null)
                    contactFormModel.LeadSource = (int)Enum.Parse(typeof(LeadSourceEnum), contact.LeadSource);

                if(contactFormModel.ContactImage != null)
                    contactFormModel.ContactImagePath = "~/Content/Upload/" + contactFormModel.ContactImage;
            }

             contactFormModel = getDropDownValues(contactFormModel);
             return View("ContactCreate", contactFormModel);
        }
        public ActionResult Details(long Id)
        {
            ContactFormModel contactDetails = new ContactFormModel();

            Contact contact = contactService.GetContact(Id);

            contactDetails = AutoMapper.Mapper.Map<Contact, ContactFormModel>(contact);

            if (contactDetails.AssignedToUserGroup == UserGroup.group)
            {
                contactDetails.AssignedToName = groupService.GetGroup(contactDetails.AssignedTo) != null ? groupService.GetGroup(contactDetails.AssignedTo).Name : string.Empty;
            }
            else
            {
                contactDetails.AssignedToName = userService.GetUser(contactDetails.AssignedTo) != null ? userService.GetUser(contactDetails.AssignedTo).Name : string.Empty;
            }

            if (contactDetails.Account != null)
                contactDetails.AccountName = accountService.GetAccount(long.Parse(contactDetails.Account.ToString())) != null ? accountService.GetAccount(long.Parse(contactDetails.Account.ToString())).AccountName : string.Empty;

            if(contactDetails.ReportsTo != null)
                contactDetails.ReportsToName = userService.GetUser(contactDetails.ReportsTo) != null ? userService.GetUser(contactDetails.ReportsTo).Name : string.Empty;

            if(contact.LeadSource != null)
                contactDetails.LeadSourceName = contact.LeadSource;

            contactDetails.ContactImagePath = "~/Content/Upload/" + contactDetails.ContactImage;

            return View("ContactDetails", contactDetails);
        }
        private ContactFormModel getDropDownValues(ContactFormModel contactFormModel)
        {
            if (contactFormModel.AssignedToUserGroup == UserGroup.group)
            {
                var groups = groupService.GetGroups();

                List<SelectListItem> grpItems = new List<SelectListItem>();

                grpItems.AddRange(groups.OrderBy(grp => grp.Name)
                .Select(grp =>
               new SelectListItem
               {
                   Selected = (grp.ID == contactFormModel.AssignedTo),
                   Text = grp.Name,
                   Value = grp.ID.ToString()
               }));

                contactFormModel.GrpUserList = grpItems;
            }
            else
            {
                contactFormModel.AssignedToUserGroup = UserGroup.user;

                var users = userService.GetUsers();

                List<SelectListItem> items = new List<SelectListItem>();
                items.AddRange(users.OrderBy(user => user.Name)
                 .Select(user =>
                new SelectListItem
                {
                    Selected = (user.ID == contactFormModel.AssignedTo),
                    Text = user.Name,
                    Value = user.ID.ToString()
                }));

                contactFormModel.GrpUserList = items;
            }

            var accounts = accountService.GetAccounts();

            List<SelectListItem> accountsitems = new List<SelectListItem>();
            accountsitems.AddRange(accounts.OrderBy(account => account.AccountName)
             .Select(account =>
            new SelectListItem
            {
                Selected = (account.ID == contactFormModel.AssignedTo),
                Text = account.AccountName,
                Value = account.ID.ToString()
            }));

            contactFormModel.AccountList = accountsitems;

            IEnumerable<LeadSourceEnum> leadSources = Enum.GetValues(typeof(LeadSourceEnum))
                                                   .Cast<LeadSourceEnum>();

            contactFormModel.LeadSourceList = from src in leadSources
                                                select new SelectListItem
                                                {
                                                    Text = src.ToString(),
                                                    Value = ((int)src).ToString()
                                                };

             var ReportTousers = userService.GetUsers();

            List<SelectListItem> ReportToitems = new List<SelectListItem>();
            ReportToitems.AddRange(ReportTousers.OrderBy(user => user.Name)
                .Select(user =>
            new SelectListItem
            {
                Selected = (user.ID == contactFormModel.ReportsTo),
                Text = user.Name,
                Value = user.ID.ToString()
            }));

            contactFormModel.ReportToList = ReportToitems;

            return contactFormModel;
        }
        public ActionResult Create(ContactFormModel contactmodel)
        {
            try
            {
                Contact contact = AutoMapper.Mapper.Map<ContactFormModel, Contact>(contactmodel);
                contact.LeadSource =((LeadSourceEnum)contactmodel.LeadSource).ToString();

                if (ModelState.IsValid)
                {
                    HttpPostedFileBase file = contactmodel.ImageUpload;

                    if (file != null)
                    {
                        if (file.ContentLength > 0)
                        {
                            int MaxContentLength = 1024 * 1024 * 3; //3 MB

                            string[] AllowedFileExtensions = new string[] { ".jpg", ".gif", ".png" };

                            if (!AllowedFileExtensions.Contains(file.FileName.Substring(file.FileName.LastIndexOf('.'))))
                            {
                                ModelState.AddModelError("ImageUpload", "Please file of type: " + string.Join(", ", AllowedFileExtensions));
                            }
                            else if (file.ContentLength > MaxContentLength)
                            {
                                ModelState.AddModelError("ImageUpload", "File is too large, maximum allowed size is: " + MaxContentLength + " MB");
                            }
                            else
                            {
                                //TO:DO
                                var fileName = Path.GetFileName(file.FileName);
                                var path = Path.Combine(Server.MapPath("~/Content/Upload"), fileName);
                                file.SaveAs(path);
                                contact.ContactImage = fileName;
                                contactmodel.ContactImage = fileName;
                                contactmodel.ContactImagePath = path;
                            }
                        }
                    }
                    if (contact.ID == 0)
                    {
                        contactService.CreateContact(contact);
                    }
                    else
                    {
                        contactService.EditContact(contact);
                    }
                    ViewBag.successMessage = "Contact Saved Successfully";
                    ViewBag.errorMessage = "";
                }
                else
                {
                    string validationErrors = string.Join(",", ModelState.Values.Where(E => E.Errors.Count > 0)
                    .SelectMany(E => E.Errors).Select(E => E.ErrorMessage).ToArray());

                    ViewBag.successMessage = "";
                    ViewBag.errorMessage = validationErrors;
                }
                contactmodel = getDropDownValues(contactmodel);
            }
            catch (Exception ex)
            {
                ViewBag.successMessage = "";
                ViewBag.errorMessage = string.Format("Error in Contact Save : {0}", ex.Message);
                contactmodel = getDropDownValues(contactmodel);
            }

            return View("ContactCreate", contactmodel);
        }