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

            ProductFormModel productFormModel = new ProductFormModel();

               if(Id != 0)
            {
                Product product = productService.GetProduct(Id);
                productFormModel = AutoMapper.Mapper.Map<Product, ProductFormModel>(product);

                productFormModel.ProductCategory = (int) Enum.Parse(typeof(ProductCategoryEnum),product.ProductCategory);
                productFormModel.Source = (int)Enum.Parse(typeof(SourceEnum), product.Source);
            }

               productFormModel = getDropDownValues(productFormModel);

               return View("ProductCreate", productFormModel);
        }
        public ActionResult Create(ProductFormModel productmodel)
        {
            try
            {
                Product product = AutoMapper.Mapper.Map<ProductFormModel, Product>(productmodel);
                product.AssignedTo = 0;
                product.ProductCategory = ((ProductCategoryEnum)productmodel.ProductCategory).ToString();
                product.Source = ((SourceEnum)productmodel.Source).ToString();
                if (ModelState.IsValid)
                {
                    if (product.ID == 0)
                    {
                        productService.CreateProduct(product);
                    }
                    else
                    {
                        productService.EditProduct(product);
                    }
                    ViewBag.successMessage = "Product 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;
                }
                productmodel = getDropDownValues(productmodel);
            }
            catch (Exception ex)
            {
                ViewBag.successMessage = "";
                ViewBag.errorMessage = string.Format("Error in Product Save : {0}", ex.Message);
                productmodel = getDropDownValues(productmodel);
            }

            return View("ProductCreate", productmodel);
        }
        private ProductFormModel getDropDownValues(ProductFormModel productFormModel)
        {
            IEnumerable<ProductCategoryEnum> categorytypes = Enum.GetValues(typeof(ProductCategoryEnum))
                                                   .Cast<ProductCategoryEnum>();

            productFormModel.ProductCategoryList = from src in categorytypes
                                          select new SelectListItem
                                          {
                                              Text = src.ToString(),
                                              Value = ((int)src).ToString()
                                          };

            IEnumerable<SourceEnum> sourceenumlist = Enum.GetValues(typeof(SourceEnum))
                                                 .Cast<SourceEnum>();

            productFormModel.SourceEnumList = from src in sourceenumlist
                                                   select new SelectListItem
                                                   {
                                                       Text = src.ToString(),
                                                       Value = ((int)src).ToString()
                                                   };

            var contacts = contactService.GetContacts();

            List<SelectListItem> contactsitems = new List<SelectListItem>();
            contactsitems.AddRange(contacts.OrderBy(cont => cont.FirstName)
                .Select(cont =>
                new SelectListItem
                {
                    Selected = (cont.ID == productFormModel.Contact),
                    Text = cont.FirstName,
                    Value = cont.ID.ToString()
                }
                ));

            productFormModel.ContactList = contactsitems;

            var vendors = vendorService.GetVendors();

            List<SelectListItem> vendorItems = new List<SelectListItem>();
            vendorItems.AddRange(vendors.OrderBy(x => x.VendorName)
                .Select(x =>
                new SelectListItem
                {
                    Selected = (x.ID == productFormModel.Vendor),
                    Text = x.VendorName,
                    Value = x.ID.ToString()
                }));

            productFormModel.VendorList = vendorItems;

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

            if(productFormModel.SourceId != 0 && productFormModel.SourceId != null)
            {
                if(productFormModel.Source == (int)SourceEnum.Leads)
                {
                    var sources = leadService.GetLeads();
                    items.AddRange(sources.OrderBy(x => x.FirstName)
                        .Select(x =>
                        new SelectListItem
                        {
                            Selected = (x.ID == productFormModel.SourceId),
                            Text = x.FirstName,
                            Value = x.ID.ToString()
                        }));
                }
                else if(productFormModel.Source == (int)SourceEnum.Potentials)
                {
                    var sources = potentialService.GetPotentials();
                    items.AddRange(sources.OrderBy(x => x.PotentialName)
                        .Select(x =>
                        new SelectListItem
                        {
                            Selected = (x.ID == productFormModel.SourceId),
                            Text = x.PotentialName,
                            Value = x.ID.ToString()
                        }));
                }
                else if(productFormModel.Source == (int)SourceEnum.Accounts)
                {
                    var sources = accountService.GetAccounts();
                    items.AddRange(sources.OrderBy(x => x.AccountName)
                        .Select(x =>
                        new SelectListItem
                        {
                            Selected = (x.ID == productFormModel.SourceId),
                            Text = x.AccountName,
                            Value = x.ID.ToString()
                        }));
                }
            }
            else
            {
                productFormModel.Source = (int)SourceEnum.Leads;

                var sources = leadService.GetLeads();
                items.AddRange(sources.OrderBy(x => x.FirstName)
                    .Select(x =>
                    new SelectListItem
                    {
                        Selected = (x.ID == productFormModel.SourceId),
                        Text = x.FirstName,
                        Value = x.ID.ToString()
                    }));
            }

            productFormModel.SourceList = items;

            return productFormModel;
        }
        public ActionResult Details(long Id)
        {
            ProductFormModel productDetails = new ProductFormModel();

            Product product = productService.GetProduct(Id);

            productDetails = AutoMapper.Mapper.Map<Product, ProductFormModel>(product);

            if (productDetails.Contact != null)
                productDetails.ContactName = contactService.GetContact(long.Parse(productDetails.Contact.ToString())) != null ? contactService.GetContact(long.Parse(productDetails.Contact.ToString())).FirstName : string.Empty;

            if (productDetails.Vendor != null)
                productDetails.VendorName = vendorService.GetVendor(long.Parse(productDetails.Vendor.ToString())) != null ? vendorService.GetVendor(long.Parse(productDetails.Vendor.ToString())).VendorName : string.Empty;

            productDetails.SourceName = product.Source;

            if (productDetails.Source == (int)SourceEnum.Leads)
            {
                productDetails.SourceIdName = leadService.GetLead(long.Parse(productDetails.SourceId.ToString())).FirstName;
            }
            else if (productDetails.Source == (int)SourceEnum.Potentials)
            {
                productDetails.SourceIdName = potentialService.GetPotential(long.Parse(productDetails.SourceId.ToString())).PotentialName;
            }
            else if (productDetails.Source == (int)SourceEnum.Accounts)
                productDetails.SourceIdName = accountService.GetAccount(long.Parse(productDetails.SourceId.ToString())).AccountName;

            productDetails.ProductCategoryName = product.ProductCategory;

            return View("ProductDetails", productDetails);
        }