Skip to main content
Post Reopened by Mast, 200_success
edited title
Link
200_success
  • 145.6k
  • 22
  • 191
  • 481

Design pattern Creating various types of menus using DIdependency injection

                    public class MyListType
                    {
                        public string ID { get; set; }
                        public string ComplexID { get; set; }
                        public string Name { get; set; }
                        public bool Selected { get; set; }
                    }


                    public class DropDownItem
                    {
                        public string Value { get; set; }
                        public string Name { get; set; }
                    }

                    public class AdminItem
                    {
                        public string ID { get; set; }
                        public string AdminName { get; set; }
                    }

                    public class MerchantItem
                    {
                        public string ID { get; set; }
                        public string MerchantName { get; set; }
                    }

                    public class CustomerItem
                    {
                        public string ID { get; set; }
                        public string CustomerName { get; set; }
                    }

                    public enum DefaultDDItems
                    {

                        ALL = 0,

                        All_Selected = 1,

                        NoDefault,

                        First_Selected,

                        CurrentMonth,

                        CurrentYear

                    }

                    public interface IDropDown
                    {
                        MultiSelectList GetMultiSelectDropDown();
                        MultiSelectList GetMultiSelectDropDown(DefaultDDItems defaultDdItem);
                        List<SelectListItem> GetSingleSelectDropDown();
                        List<SelectListItem> GetSingleSelectDropDown(DefaultDDItems defaultDdItem);
                    }

                    public class AdminLevelDropDown : IDropDown
                    {
                        private List<AdminItem> _adminItemes;
                        private string[] _selectedIds;

                        public AdminLevelDropDown()
                        {
                        }

                        public AdminLevelDropDown(List<AdminItem> items)
                        {
                            this._adminItemes = items;
                        }

                        public AdminLevelDropDown(List<AdminItem> items, string[] defaultSelectedItems)
                        {
                            this._adminItemes = items;
                            this._selectedIds = defaultSelectedItems;
                        }

                        public MultiSelectList GetMultiSelectDropDown()
                        {
                            return GetMultiSelectDropDown(DefaultDDItems.NoDefault);
                        }

                        public MultiSelectList GetMultiSelectDropDown(DefaultDDItems defaultDdItem)
                        {
                            var mylist = new List<MyListType>();

                            if (_adminItemes == null)
                            {
                                return new MultiSelectList(mylist, "ID", "Name");
                            }

                            if (!_adminItemes.Any())
                            {
                                return new MultiSelectList(mylist, "ID", "Name");
                            }

                            switch (defaultDdItem)
                            {
                                case DefaultDDItems.ALL:
                                    mylist.Add(new MyListType()
                                        {
                                            Name = "All",
                                            ID = "0"
                                        });
                                    break;

                                case DefaultDDItems.All_Selected:
                                    mylist.Add(new MyListType()
                                        {
                                            Name = "All",
                                            ID = String.Join(",", _adminItemes)
                                        });
                                    break;
                                  // Other switch cases can be implemented here
                            }

                            mylist.AddRange(_adminItemes.Select(i => new MyListType()
                                {
                                    Name = i.AdminName,
                                    ID = i.ID
                                }).ToList());

                            return new MultiSelectList(mylist, "ID", "Name", _selectedIds);

                        }

                        public List<SelectListItem> GetSingleSelectDropDown()
                        {
                            return GetSingleSelectDropDown(DefaultDDItems.NoDefault);
                        }

                        public List<SelectListItem> GetSingleSelectDropDown(DefaultDDItems defaultDdItem)
                        {
                            var mylist = new List<SelectListItem>();

                            if (_adminItemes == null)
                            {
                                return new List<SelectListItem>(0);
                            }

                            if (!_adminItemes.Any())
                            {
                                return new List<SelectListItem>(0);
                            }

                            switch (defaultDdItem)
                            {
                                case DefaultDDItems.ALL:
                                    mylist.Add(new SelectListItem()
                                    {
                                        Value = "All",
                                        Text = "0"
                                    });
                                    break;

                                case DefaultDDItems.All_Selected:
                                    mylist.Add(new SelectListItem()
                                    {
                                        Value = "All",
                                        Text = String.Join(",", _adminItemes)
                                    });
                                    break;
                            }

                            mylist.AddRange(_adminItemes.Select(i => new SelectListItem()
                            {
                                Text = i.AdminName,
                                Value = i.ID,
                                Selected = _selectedIds.FirstOrDefault() == i.ID
                            }).ToList());

                            return mylist;
                        }
                    }

                    public class MerchantLevelDropDown : IDropDown
                    {

                        private List<MerchantItem> _merchantItemes;
                        private string[] _selectedIds;


                        public MerchantLevelDropDown()
                        {
                        }

                        public MerchantLevelDropDown(List<MerchantItem> items)
                        {
                            this._merchantItemes = items;
                        }

                        public MerchantLevelDropDown(List<MerchantItem> items, string[] defaultSelectedItems)
                        {
                            this._merchantItemes = items;
                            this._selectedIds = defaultSelectedItems;
                        }


                        public MultiSelectList GetMultiSelectDropDown()
                        {
                            return GetMultiSelectDropDown(DefaultDDItems.NoDefault);
                        }

                        public MultiSelectList GetMultiSelectDropDown(DefaultDDItems defaultDdItem)
                        {
                            var mylist = new List<MyListType>();

                            if (_merchantItemes == null)
                            {
                                return new MultiSelectList(mylist, "ID", "Name");
                            }

                            if (!_merchantItemes.Any())
                            {
                                return new MultiSelectList(mylist, "ID", "Name");
                            }

                            switch (defaultDdItem)
                            {
                                case DefaultDDItems.ALL:
                                    mylist.Add(new MyListType()
                                    {
                                        Name = "All",
                                        ID = "0"
                                    });
                                    break;

                                case DefaultDDItems.All_Selected:
                                    mylist.Add(new MyListType()
                                    {
                                        Name = "All",
                                        ID = String.Join(",", _merchantItemes)
                                    });
                                    break;
                            }

                            mylist.AddRange(_merchantItemes.Select(i => new MyListType()
                            {
                                Name = i.MerchantName,
                                ID = i.ID
                            }).ToList());

                            return new MultiSelectList(mylist, "ID", "Name", _selectedIds);
                        }

                        public List<SelectListItem> GetSingleSelectDropDown()
                        {
                            return GetSingleSelectDropDown(DefaultDDItems.NoDefault);
                        }

                        public List<SelectListItem> GetSingleSelectDropDown(DefaultDDItems defaultDdItem)
                        {
                            var mylist = new List<SelectListItem>();

                            if (_merchantItemes == null)
                            {
                                return new List<SelectListItem>(0);
                            }

                            if (!_merchantItemes.Any())
                            {
                                return new List<SelectListItem>(0);
                            }

                            switch (defaultDdItem)
                            {
                                case DefaultDDItems.ALL:
                                    mylist.Add(new SelectListItem()
                                    {
                                        Value = "All",
                                        Text = "0"
                                    });
                                    break;

                                case DefaultDDItems.All_Selected:
                                    mylist.Add(new SelectListItem()
                                    {
                                        Value = "All",
                                        Text = String.Join(",", _merchantItemes)
                                    });
                                    break;
                            }

                            mylist.AddRange(_merchantItemes.Select(i => new SelectListItem()
                            {
                                Text = i.MerchantName,
                                Value = i.ID,
                                Selected = _selectedIds.FirstOrDefault() == i.ID
                            }).ToList());

                            return mylist;
                        }
                    }

                    public class CustomerLevelDropDown : IDropDown
                    {

                        private List<CustomerItem> _customerItemes;
                        private string[] _selectedIds;


                        public CustomerLevelDropDown()
                        {
                        }

                        public CustomerLevelDropDown(List<CustomerItem> items)
                        {
                            this._customerItemes = items;
                        }

                        public CustomerLevelDropDown(List<CustomerItem> items, string[] defaultSelectedItems)
                        {
                            this._customerItemes = items;
                            this._selectedIds = defaultSelectedItems;
                        }


                        public MultiSelectList GetMultiSelectDropDown()
                        {
                            return GetMultiSelectDropDown(DefaultDDItems.NoDefault);
                        }

                        public MultiSelectList GetMultiSelectDropDown(DefaultDDItems defaultDdItem)
                        {
                            var mylist = new List<MyListType>();

                            if (_customerItemes == null)
                            {
                                return new MultiSelectList(mylist, "ID", "Name");
                            }

                            if (!_customerItemes.Any())
                            {
                                return new MultiSelectList(mylist, "ID", "Name");
                            }

                            switch (defaultDdItem)
                            {
                                case DefaultDDItems.ALL:
                                    mylist.Add(new MyListType()
                                    {
                                        Name = "All",
                                        ID = "0"
                                    });
                                    break;

                                case DefaultDDItems.All_Selected:
                                    mylist.Add(new MyListType()
                                    {
                                        Name = "All",
                                        ID = String.Join(",", _customerItemes)
                                    });
                                    break;
                            }

                            mylist.AddRange(_customerItemes.Select(i => new MyListType()
                            {
                                Name = i.CustomerName,
                                ID = i.ID
                            }).ToList());

                            return new MultiSelectList(mylist, "ID", "Name", _selectedIds);
                        }

                        public List<SelectListItem> GetSingleSelectDropDown()
                        {
                            return GetSingleSelectDropDown(DefaultDDItems.NoDefault);
                        }

                        public List<SelectListItem> GetSingleSelectDropDown(DefaultDDItems defaultDdItem)
                        {
                            var mylist = new List<SelectListItem>();

                            if (_customerItemes == null)
                            {
                                return new List<SelectListItem>(0);
                            }

                            if (!_customerItemes.Any())
                            {
                                return new List<SelectListItem>(0);
                            }

                            switch (defaultDdItem)
                            {
                                case DefaultDDItems.ALL:
                                    mylist.Add(new SelectListItem()
                                    {
                                        Value = "All",
                                        Text = "0"
                                    });
                                    break;

                                case DefaultDDItems.All_Selected:
                                    mylist.Add(new SelectListItem()
                                    {
                                        Value = "All",
                                        Text = String.Join(",", _customerItemes)
                                    });
                                    break;
                            }

                            mylist.AddRange(_customerItemes.Select(i => new SelectListItem()
                            {
                                Text = i.CustomerName,
                                Value = i.ID,
                                Selected = _selectedIds.FirstOrDefault() == i.ID
                            }).ToList());

                            return mylist;
                        }
                    }


                    public interface IDropdownFactory
                    {
                        AdminLevelDropDown GetMultiSelect_AdminDropDown();
                        AdminLevelDropDown GetMultiSelect_AdminDropDown(List<AdminItem> items);
                        AdminLevelDropDown GetMultiSelect_AdminDropDown(List<AdminItem> items, string[] selecteditems);

                        AdminLevelDropDown GetSingleSelect_AdminDropDown();
                        AdminLevelDropDown GetSingleSelect_AdminDropDown(List<AdminItem> items);
                        AdminLevelDropDown GetSingleSelect_AdminDropDown(List<AdminItem> items, string[] selectedItems);




                        MerchantLevelDropDown GetMultiSelect_MerchantDropDown();
                        MerchantLevelDropDown GetMultiSelect_MerchantDropDown(List<MerchantItem> items);
                        MerchantLevelDropDown GetMultiSelect_MerchantDropDown(List<MerchantItem> items, string[] selecteditems);

                        MerchantLevelDropDown GetSingleSelect_MerchantDropDown();
                        MerchantLevelDropDown GetSingleSelect_MerchantDropDown(List<MerchantItem> items);
                        MerchantLevelDropDown GetSingleSelect_MerchantDropDown(List<MerchantItem> items, string[] selectedItems);



                        CustomerLevelDropDown GetMultiSelect_CustomerDropDown();
                        CustomerLevelDropDown GetMultiSelect_CustomerDropDown(List<CustomerItem> items);
                        CustomerLevelDropDown GetMultiSelect_CustomerDropDown(List<CustomerItem> items, string[] selecteditems);

                        CustomerLevelDropDown GetSingleSelect_CustomerDropDown();
                        CustomerLevelDropDown GetSingleSelect_CustomerDropDown(List<CustomerItem> items);
                        CustomerLevelDropDown GetSingleSelect_CustomerDropDown(List<CustomerItem> items, string[] selectedItems);

                    }

                    public class DropDownFactory : IDropdownFactory
                    {

                        public AdminLevelDropDown GetMultiSelect_AdminDropDown()
                        {
                            return new AdminLevelDropDown();
                        }

                        public AdminLevelDropDown GetMultiSelect_AdminDropDown(List<AdminItem> items)
                        {
                            return new AdminLevelDropDown(items);
                        }

                        public AdminLevelDropDown GetMultiSelect_AdminDropDown(List<AdminItem> items, string[] selecteditems)
                        {
                            return new AdminLevelDropDown(items, selecteditems);
                        }

                        public AdminLevelDropDown GetSingleSelect_AdminDropDown()
                        {
                            return new AdminLevelDropDown();
                        }

                        public AdminLevelDropDown GetSingleSelect_AdminDropDown(List<AdminItem> items)
                        {
                            return new AdminLevelDropDown(items);
                        }

                        public AdminLevelDropDown GetSingleSelect_AdminDropDown(List<AdminItem> items, string[] selectedItems)
                        {
                            return new AdminLevelDropDown(items, selectedItems);
                        }

                        public MerchantLevelDropDown GetMultiSelect_MerchantDropDown()
                        {
                            return new MerchantLevelDropDown();
                        }

                        public MerchantLevelDropDown GetMultiSelect_MerchantDropDown(List<MerchantItem> items)
                        {
                            return new MerchantLevelDropDown(items);
                        }

                        public MerchantLevelDropDown GetMultiSelect_MerchantDropDown(List<MerchantItem> items, string[] selecteditems)
                        {
                            return new MerchantLevelDropDown(items, selecteditems);
                        }

                        public MerchantLevelDropDown GetSingleSelect_MerchantDropDown()
                        {
                            return new MerchantLevelDropDown();
                        }

                        public MerchantLevelDropDown GetSingleSelect_MerchantDropDown(List<MerchantItem> items)
                        {
                            return new MerchantLevelDropDown(items);
                        }

                        public MerchantLevelDropDown GetSingleSelect_MerchantDropDown(List<MerchantItem> items, string[] selectedItems)
                        {
                            return new MerchantLevelDropDown(items, selectedItems);
                        }

                        public CustomerLevelDropDown GetMultiSelect_CustomerDropDown()
                        {
                            return new CustomerLevelDropDown();
                        }

                        public CustomerLevelDropDown GetMultiSelect_CustomerDropDown(List<CustomerItem> items)
                        {
                            return new CustomerLevelDropDown(items);
                        }

                        public CustomerLevelDropDown GetMultiSelect_CustomerDropDown(List<CustomerItem> items, string[] selecteditems)
                        {
                            return new CustomerLevelDropDown(items, selecteditems);
                        }

                        public CustomerLevelDropDown GetSingleSelect_CustomerDropDown()
                        {
                            return new CustomerLevelDropDown();
                        }

                        public CustomerLevelDropDown GetSingleSelect_CustomerDropDown(List<CustomerItem> items)
                        {
                            return new CustomerLevelDropDown(items);
                        }

                        public CustomerLevelDropDown GetSingleSelect_CustomerDropDown(List<CustomerItem> items, string[] selectedItems)
                        {
                            return new CustomerLevelDropDown(items, selectedItems);
                        }
                    }
                            var container = new Container();
                            container.RegisterType<DropDownFactory>().As<IDropdownFactory>().InstancePerHttpRequest();

                
                public class HomeController : Controller
                {
                    public IDropdownFactory _dropdownFactory;

                    public HomeController(IDropdownFactory dropdownFactory)
                    {
                        this._dropdownFactory = dropdownFactory;
                    }

                    public ViewResult GetMultiSelect_AdminDropDown(string[] selectedids)
                    {
                        var adminItems = _adminRepo.GetAdminItems();

                        var adminDD = _dropdownFactory.GetMultiSelect_AdminDropDown(adminItems, selectedids);
                        return View(adminDD);

                    }

                    public ViewResult GetSingleSelect_AdminDropDown(string[] selectedids)
                    {
                        var adminItems = _adminRepo.GetAdminItems();

                        var adminDD = _dropdownFactory.GetSingleSelect_AdminDropDown(adminItems, selectedids);
                        return View(adminDD);

                    }

                    public ViewResult GetMultiSelect_MerchantDropDown(string[] selectedids)
                    {
                        var merchantItems = _merchantRepo.GetMerchantItems();

                        var merchantDD = _dropdownFactory.GetMultiSelect_MerchantDropDown(merchantItems, selectedids);
                        return View(merchantDD);
                    }

                    public ViewResult GetSingleSelect_MerchantDropDown(string[] selectedids)
                    {
                        var merchantItems = _merchantRepo.GetMerchantItems();

                        var merchantDD = _dropdownFactory.GetSingleSelect_MerchantDropDown(merchantItems, selectedids);
                        return View(merchantDD);
                    }

                    public ViewResult GetMultiSelect_CustomerDropDown(string[] selectedids)
                    {
                        var customerItems = _customerRepo.GetCustomerItems();

                        var merchantDD = _dropdownFactory.GetMultiSelect_CustomerDropDown(customerItems, selectedids);
                        return View(merchantDD);
                    }

                    public ViewResult GetSingleSelect_CustomerDropDown(string[] selectedids)
                    {
                        var customerItems = _customerRepo.GetCustomerItems();

                        var merchantDD = _dropdownFactory.GetSingleSelect_CustomerDropDown(customerItems, selectedids);
                        return View(merchantDD);
                    }

                }
                    public class MyListType
                    {
                        public string ID { get; set; }
                        public string ComplexID { get; set; }
                        public string Name { get; set; }
                        public bool Selected { get; set; }
                    }


                    public class DropDownItem
                    {
                        public string Value { get; set; }
                        public string Name { get; set; }
                    }

                    public class AdminItem
                    {
                        public string ID { get; set; }
                        public string AdminName { get; set; }
                    }

                    public class MerchantItem
                    {
                        public string ID { get; set; }
                        public string MerchantName { get; set; }
                    }

                    public class CustomerItem
                    {
                        public string ID { get; set; }
                        public string CustomerName { get; set; }
                    }

                    public enum DefaultDDItems
                    {

                        ALL = 0,

                        All_Selected = 1,

                        NoDefault,

                        First_Selected,

                        CurrentMonth,

                        CurrentYear

                    }

                    public interface IDropDown
                    {
                        MultiSelectList GetMultiSelectDropDown();
                        MultiSelectList GetMultiSelectDropDown(DefaultDDItems defaultDdItem);
                        List<SelectListItem> GetSingleSelectDropDown();
                        List<SelectListItem> GetSingleSelectDropDown(DefaultDDItems defaultDdItem);
                    }

                    public class AdminLevelDropDown : IDropDown
                    {
                        private List<AdminItem> _adminItemes;
                        private string[] _selectedIds;

                        public AdminLevelDropDown()
                        {
                        }

                        public AdminLevelDropDown(List<AdminItem> items)
                        {
                            this._adminItemes = items;
                        }

                        public AdminLevelDropDown(List<AdminItem> items, string[] defaultSelectedItems)
                        {
                            this._adminItemes = items;
                            this._selectedIds = defaultSelectedItems;
                        }

                        public MultiSelectList GetMultiSelectDropDown()
                        {
                            return GetMultiSelectDropDown(DefaultDDItems.NoDefault);
                        }

                        public MultiSelectList GetMultiSelectDropDown(DefaultDDItems defaultDdItem)
                        {
                            var mylist = new List<MyListType>();

                            if (_adminItemes == null)
                            {
                                return new MultiSelectList(mylist, "ID", "Name");
                            }

                            if (!_adminItemes.Any())
                            {
                                return new MultiSelectList(mylist, "ID", "Name");
                            }

                            switch (defaultDdItem)
                            {
                                case DefaultDDItems.ALL:
                                    mylist.Add(new MyListType()
                                        {
                                            Name = "All",
                                            ID = "0"
                                        });
                                    break;

                                case DefaultDDItems.All_Selected:
                                    mylist.Add(new MyListType()
                                        {
                                            Name = "All",
                                            ID = String.Join(",", _adminItemes)
                                        });
                                    break;
                                  // Other switch cases can be implemented here
                            }

                            mylist.AddRange(_adminItemes.Select(i => new MyListType()
                                {
                                    Name = i.AdminName,
                                    ID = i.ID
                                }).ToList());

                            return new MultiSelectList(mylist, "ID", "Name", _selectedIds);

                        }

                        public List<SelectListItem> GetSingleSelectDropDown()
                        {
                            return GetSingleSelectDropDown(DefaultDDItems.NoDefault);
                        }

                        public List<SelectListItem> GetSingleSelectDropDown(DefaultDDItems defaultDdItem)
                        {
                            var mylist = new List<SelectListItem>();

                            if (_adminItemes == null)
                            {
                                return new List<SelectListItem>(0);
                            }

                            if (!_adminItemes.Any())
                            {
                                return new List<SelectListItem>(0);
                            }

                            switch (defaultDdItem)
                            {
                                case DefaultDDItems.ALL:
                                    mylist.Add(new SelectListItem()
                                    {
                                        Value = "All",
                                        Text = "0"
                                    });
                                    break;

                                case DefaultDDItems.All_Selected:
                                    mylist.Add(new SelectListItem()
                                    {
                                        Value = "All",
                                        Text = String.Join(",", _adminItemes)
                                    });
                                    break;
                            }

                            mylist.AddRange(_adminItemes.Select(i => new SelectListItem()
                            {
                                Text = i.AdminName,
                                Value = i.ID,
                                Selected = _selectedIds.FirstOrDefault() == i.ID
                            }).ToList());

                            return mylist;
                        }
                    }

                    public class MerchantLevelDropDown : IDropDown
                    {

                        private List<MerchantItem> _merchantItemes;
                        private string[] _selectedIds;


                        public MerchantLevelDropDown()
                        {
                        }

                        public MerchantLevelDropDown(List<MerchantItem> items)
                        {
                            this._merchantItemes = items;
                        }

                        public MerchantLevelDropDown(List<MerchantItem> items, string[] defaultSelectedItems)
                        {
                            this._merchantItemes = items;
                            this._selectedIds = defaultSelectedItems;
                        }


                        public MultiSelectList GetMultiSelectDropDown()
                        {
                            return GetMultiSelectDropDown(DefaultDDItems.NoDefault);
                        }

                        public MultiSelectList GetMultiSelectDropDown(DefaultDDItems defaultDdItem)
                        {
                            var mylist = new List<MyListType>();

                            if (_merchantItemes == null)
                            {
                                return new MultiSelectList(mylist, "ID", "Name");
                            }

                            if (!_merchantItemes.Any())
                            {
                                return new MultiSelectList(mylist, "ID", "Name");
                            }

                            switch (defaultDdItem)
                            {
                                case DefaultDDItems.ALL:
                                    mylist.Add(new MyListType()
                                    {
                                        Name = "All",
                                        ID = "0"
                                    });
                                    break;

                                case DefaultDDItems.All_Selected:
                                    mylist.Add(new MyListType()
                                    {
                                        Name = "All",
                                        ID = String.Join(",", _merchantItemes)
                                    });
                                    break;
                            }

                            mylist.AddRange(_merchantItemes.Select(i => new MyListType()
                            {
                                Name = i.MerchantName,
                                ID = i.ID
                            }).ToList());

                            return new MultiSelectList(mylist, "ID", "Name", _selectedIds);
                        }

                        public List<SelectListItem> GetSingleSelectDropDown()
                        {
                            return GetSingleSelectDropDown(DefaultDDItems.NoDefault);
                        }

                        public List<SelectListItem> GetSingleSelectDropDown(DefaultDDItems defaultDdItem)
                        {
                            var mylist = new List<SelectListItem>();

                            if (_merchantItemes == null)
                            {
                                return new List<SelectListItem>(0);
                            }

                            if (!_merchantItemes.Any())
                            {
                                return new List<SelectListItem>(0);
                            }

                            switch (defaultDdItem)
                            {
                                case DefaultDDItems.ALL:
                                    mylist.Add(new SelectListItem()
                                    {
                                        Value = "All",
                                        Text = "0"
                                    });
                                    break;

                                case DefaultDDItems.All_Selected:
                                    mylist.Add(new SelectListItem()
                                    {
                                        Value = "All",
                                        Text = String.Join(",", _merchantItemes)
                                    });
                                    break;
                            }

                            mylist.AddRange(_merchantItemes.Select(i => new SelectListItem()
                            {
                                Text = i.MerchantName,
                                Value = i.ID,
                                Selected = _selectedIds.FirstOrDefault() == i.ID
                            }).ToList());

                            return mylist;
                        }
                    }

                    public class CustomerLevelDropDown : IDropDown
                    {

                        private List<CustomerItem> _customerItemes;
                        private string[] _selectedIds;


                        public CustomerLevelDropDown()
                        {
                        }

                        public CustomerLevelDropDown(List<CustomerItem> items)
                        {
                            this._customerItemes = items;
                        }

                        public CustomerLevelDropDown(List<CustomerItem> items, string[] defaultSelectedItems)
                        {
                            this._customerItemes = items;
                            this._selectedIds = defaultSelectedItems;
                        }


                        public MultiSelectList GetMultiSelectDropDown()
                        {
                            return GetMultiSelectDropDown(DefaultDDItems.NoDefault);
                        }

                        public MultiSelectList GetMultiSelectDropDown(DefaultDDItems defaultDdItem)
                        {
                            var mylist = new List<MyListType>();

                            if (_customerItemes == null)
                            {
                                return new MultiSelectList(mylist, "ID", "Name");
                            }

                            if (!_customerItemes.Any())
                            {
                                return new MultiSelectList(mylist, "ID", "Name");
                            }

                            switch (defaultDdItem)
                            {
                                case DefaultDDItems.ALL:
                                    mylist.Add(new MyListType()
                                    {
                                        Name = "All",
                                        ID = "0"
                                    });
                                    break;

                                case DefaultDDItems.All_Selected:
                                    mylist.Add(new MyListType()
                                    {
                                        Name = "All",
                                        ID = String.Join(",", _customerItemes)
                                    });
                                    break;
                            }

                            mylist.AddRange(_customerItemes.Select(i => new MyListType()
                            {
                                Name = i.CustomerName,
                                ID = i.ID
                            }).ToList());

                            return new MultiSelectList(mylist, "ID", "Name", _selectedIds);
                        }

                        public List<SelectListItem> GetSingleSelectDropDown()
                        {
                            return GetSingleSelectDropDown(DefaultDDItems.NoDefault);
                        }

                        public List<SelectListItem> GetSingleSelectDropDown(DefaultDDItems defaultDdItem)
                        {
                            var mylist = new List<SelectListItem>();

                            if (_customerItemes == null)
                            {
                                return new List<SelectListItem>(0);
                            }

                            if (!_customerItemes.Any())
                            {
                                return new List<SelectListItem>(0);
                            }

                            switch (defaultDdItem)
                            {
                                case DefaultDDItems.ALL:
                                    mylist.Add(new SelectListItem()
                                    {
                                        Value = "All",
                                        Text = "0"
                                    });
                                    break;

                                case DefaultDDItems.All_Selected:
                                    mylist.Add(new SelectListItem()
                                    {
                                        Value = "All",
                                        Text = String.Join(",", _customerItemes)
                                    });
                                    break;
                            }

                            mylist.AddRange(_customerItemes.Select(i => new SelectListItem()
                            {
                                Text = i.CustomerName,
                                Value = i.ID,
                                Selected = _selectedIds.FirstOrDefault() == i.ID
                            }).ToList());

                            return mylist;
                        }
                    }


                    public interface IDropdownFactory
                    {
                        AdminLevelDropDown GetMultiSelect_AdminDropDown();
                        AdminLevelDropDown GetMultiSelect_AdminDropDown(List<AdminItem> items);
                        AdminLevelDropDown GetMultiSelect_AdminDropDown(List<AdminItem> items, string[] selecteditems);

                        AdminLevelDropDown GetSingleSelect_AdminDropDown();
                        AdminLevelDropDown GetSingleSelect_AdminDropDown(List<AdminItem> items);
                        AdminLevelDropDown GetSingleSelect_AdminDropDown(List<AdminItem> items, string[] selectedItems);




                        MerchantLevelDropDown GetMultiSelect_MerchantDropDown();
                        MerchantLevelDropDown GetMultiSelect_MerchantDropDown(List<MerchantItem> items);
                        MerchantLevelDropDown GetMultiSelect_MerchantDropDown(List<MerchantItem> items, string[] selecteditems);

                        MerchantLevelDropDown GetSingleSelect_MerchantDropDown();
                        MerchantLevelDropDown GetSingleSelect_MerchantDropDown(List<MerchantItem> items);
                        MerchantLevelDropDown GetSingleSelect_MerchantDropDown(List<MerchantItem> items, string[] selectedItems);



                        CustomerLevelDropDown GetMultiSelect_CustomerDropDown();
                        CustomerLevelDropDown GetMultiSelect_CustomerDropDown(List<CustomerItem> items);
                        CustomerLevelDropDown GetMultiSelect_CustomerDropDown(List<CustomerItem> items, string[] selecteditems);

                        CustomerLevelDropDown GetSingleSelect_CustomerDropDown();
                        CustomerLevelDropDown GetSingleSelect_CustomerDropDown(List<CustomerItem> items);
                        CustomerLevelDropDown GetSingleSelect_CustomerDropDown(List<CustomerItem> items, string[] selectedItems);

                    }

                    public class DropDownFactory : IDropdownFactory
                    {

                        public AdminLevelDropDown GetMultiSelect_AdminDropDown()
                        {
                            return new AdminLevelDropDown();
                        }

                        public AdminLevelDropDown GetMultiSelect_AdminDropDown(List<AdminItem> items)
                        {
                            return new AdminLevelDropDown(items);
                        }

                        public AdminLevelDropDown GetMultiSelect_AdminDropDown(List<AdminItem> items, string[] selecteditems)
                        {
                            return new AdminLevelDropDown(items, selecteditems);
                        }

                        public AdminLevelDropDown GetSingleSelect_AdminDropDown()
                        {
                            return new AdminLevelDropDown();
                        }

                        public AdminLevelDropDown GetSingleSelect_AdminDropDown(List<AdminItem> items)
                        {
                            return new AdminLevelDropDown(items);
                        }

                        public AdminLevelDropDown GetSingleSelect_AdminDropDown(List<AdminItem> items, string[] selectedItems)
                        {
                            return new AdminLevelDropDown(items, selectedItems);
                        }

                        public MerchantLevelDropDown GetMultiSelect_MerchantDropDown()
                        {
                            return new MerchantLevelDropDown();
                        }

                        public MerchantLevelDropDown GetMultiSelect_MerchantDropDown(List<MerchantItem> items)
                        {
                            return new MerchantLevelDropDown(items);
                        }

                        public MerchantLevelDropDown GetMultiSelect_MerchantDropDown(List<MerchantItem> items, string[] selecteditems)
                        {
                            return new MerchantLevelDropDown(items, selecteditems);
                        }

                        public MerchantLevelDropDown GetSingleSelect_MerchantDropDown()
                        {
                            return new MerchantLevelDropDown();
                        }

                        public MerchantLevelDropDown GetSingleSelect_MerchantDropDown(List<MerchantItem> items)
                        {
                            return new MerchantLevelDropDown(items);
                        }

                        public MerchantLevelDropDown GetSingleSelect_MerchantDropDown(List<MerchantItem> items, string[] selectedItems)
                        {
                            return new MerchantLevelDropDown(items, selectedItems);
                        }

                        public CustomerLevelDropDown GetMultiSelect_CustomerDropDown()
                        {
                            return new CustomerLevelDropDown();
                        }

                        public CustomerLevelDropDown GetMultiSelect_CustomerDropDown(List<CustomerItem> items)
                        {
                            return new CustomerLevelDropDown(items);
                        }

                        public CustomerLevelDropDown GetMultiSelect_CustomerDropDown(List<CustomerItem> items, string[] selecteditems)
                        {
                            return new CustomerLevelDropDown(items, selecteditems);
                        }

                        public CustomerLevelDropDown GetSingleSelect_CustomerDropDown()
                        {
                            return new CustomerLevelDropDown();
                        }

                        public CustomerLevelDropDown GetSingleSelect_CustomerDropDown(List<CustomerItem> items)
                        {
                            return new CustomerLevelDropDown(items);
                        }

                        public CustomerLevelDropDown GetSingleSelect_CustomerDropDown(List<CustomerItem> items, string[] selectedItems)
                        {
                            return new CustomerLevelDropDown(items, selectedItems);
                        }
                    }
                            var container = new Container();
                            container.RegisterType<DropDownFactory>().As<IDropdownFactory>().InstancePerHttpRequest();

                
                public class HomeController : Controller
                {
                    public IDropdownFactory _dropdownFactory;

                    public HomeController(IDropdownFactory dropdownFactory)
                    {
                        this._dropdownFactory = dropdownFactory;
                    }

                    public ViewResult GetMultiSelect_AdminDropDown(string[] selectedids)
                    {
                        var adminItems = _adminRepo.GetAdminItems();

                        var adminDD = _dropdownFactory.GetMultiSelect_AdminDropDown(adminItems, selectedids);
                        return View(adminDD);

                    }

                    public ViewResult GetSingleSelect_AdminDropDown(string[] selectedids)
                    {
                        var adminItems = _adminRepo.GetAdminItems();

                        var adminDD = _dropdownFactory.GetSingleSelect_AdminDropDown(adminItems, selectedids);
                        return View(adminDD);

                    }

                    public ViewResult GetMultiSelect_MerchantDropDown(string[] selectedids)
                    {
                        var merchantItems = _merchantRepo.GetMerchantItems();

                        var merchantDD = _dropdownFactory.GetMultiSelect_MerchantDropDown(merchantItems, selectedids);
                        return View(merchantDD);
                    }

                    public ViewResult GetSingleSelect_MerchantDropDown(string[] selectedids)
                    {
                        var merchantItems = _merchantRepo.GetMerchantItems();

                        var merchantDD = _dropdownFactory.GetSingleSelect_MerchantDropDown(merchantItems, selectedids);
                        return View(merchantDD);
                    }

                    public ViewResult GetMultiSelect_CustomerDropDown(string[] selectedids)
                    {
                        var customerItems = _customerRepo.GetCustomerItems();

                        var merchantDD = _dropdownFactory.GetMultiSelect_CustomerDropDown(customerItems, selectedids);
                        return View(merchantDD);
                    }

                    public ViewResult GetSingleSelect_CustomerDropDown(string[] selectedids)
                    {
                        var customerItems = _customerRepo.GetCustomerItems();

                        var merchantDD = _dropdownFactory.GetSingleSelect_CustomerDropDown(customerItems, selectedids);
                        return View(merchantDD);
                    }

                }
public class MyListType
{
    public string ID { get; set; }
    public string ComplexID { get; set; }
    public string Name { get; set; }
    public bool Selected { get; set; }
}


public class DropDownItem
{
    public string Value { get; set; }
    public string Name { get; set; }
}

public class AdminItem
{
    public string ID { get; set; }
    public string AdminName { get; set; }
}

public class MerchantItem
{
    public string ID { get; set; }
    public string MerchantName { get; set; }
}

public class CustomerItem
{
    public string ID { get; set; }
    public string CustomerName { get; set; }
}

public enum DefaultDDItems
{

    ALL = 0,

    All_Selected = 1,

    NoDefault,

    First_Selected,

    CurrentMonth,

    CurrentYear

}

public interface IDropDown
{
    MultiSelectList GetMultiSelectDropDown();
    MultiSelectList GetMultiSelectDropDown(DefaultDDItems defaultDdItem);
    List<SelectListItem> GetSingleSelectDropDown();
    List<SelectListItem> GetSingleSelectDropDown(DefaultDDItems defaultDdItem);
}

public class AdminLevelDropDown : IDropDown
{
    private List<AdminItem> _adminItemes;
    private string[] _selectedIds;

    public AdminLevelDropDown()
    {
    }

    public AdminLevelDropDown(List<AdminItem> items)
    {
        this._adminItemes = items;
    }

    public AdminLevelDropDown(List<AdminItem> items, string[] defaultSelectedItems)
    {
        this._adminItemes = items;
        this._selectedIds = defaultSelectedItems;
    }

    public MultiSelectList GetMultiSelectDropDown()
    {
        return GetMultiSelectDropDown(DefaultDDItems.NoDefault);
    }

    public MultiSelectList GetMultiSelectDropDown(DefaultDDItems defaultDdItem)
    {
        var mylist = new List<MyListType>();

        if (_adminItemes == null)
        {
            return new MultiSelectList(mylist, "ID", "Name");
        }

        if (!_adminItemes.Any())
        {
            return new MultiSelectList(mylist, "ID", "Name");
        }

        switch (defaultDdItem)
        {
            case DefaultDDItems.ALL:
                mylist.Add(new MyListType()
                    {
                        Name = "All",
                        ID = "0"
                    });
                break;

            case DefaultDDItems.All_Selected:
                mylist.Add(new MyListType()
                    {
                        Name = "All",
                        ID = String.Join(",", _adminItemes)
                    });
                break;
              // Other switch cases can be implemented here
        }

        mylist.AddRange(_adminItemes.Select(i => new MyListType()
            {
                Name = i.AdminName,
                ID = i.ID
            }).ToList());

        return new MultiSelectList(mylist, "ID", "Name", _selectedIds);

    }

    public List<SelectListItem> GetSingleSelectDropDown()
    {
        return GetSingleSelectDropDown(DefaultDDItems.NoDefault);
    }

    public List<SelectListItem> GetSingleSelectDropDown(DefaultDDItems defaultDdItem)
    {
        var mylist = new List<SelectListItem>();

        if (_adminItemes == null)
        {
            return new List<SelectListItem>(0);
        }

        if (!_adminItemes.Any())
        {
            return new List<SelectListItem>(0);
        }

        switch (defaultDdItem)
        {
            case DefaultDDItems.ALL:
                mylist.Add(new SelectListItem()
                {
                    Value = "All",
                    Text = "0"
                });
                break;

            case DefaultDDItems.All_Selected:
                mylist.Add(new SelectListItem()
                {
                    Value = "All",
                    Text = String.Join(",", _adminItemes)
                });
                break;
        }

        mylist.AddRange(_adminItemes.Select(i => new SelectListItem()
        {
            Text = i.AdminName,
            Value = i.ID,
            Selected = _selectedIds.FirstOrDefault() == i.ID
        }).ToList());

        return mylist;
    }
}

public class MerchantLevelDropDown : IDropDown
{

    private List<MerchantItem> _merchantItemes;
    private string[] _selectedIds;


    public MerchantLevelDropDown()
    {
    }

    public MerchantLevelDropDown(List<MerchantItem> items)
    {
        this._merchantItemes = items;
    }

    public MerchantLevelDropDown(List<MerchantItem> items, string[] defaultSelectedItems)
    {
        this._merchantItemes = items;
        this._selectedIds = defaultSelectedItems;
    }


    public MultiSelectList GetMultiSelectDropDown()
    {
        return GetMultiSelectDropDown(DefaultDDItems.NoDefault);
    }

    public MultiSelectList GetMultiSelectDropDown(DefaultDDItems defaultDdItem)
    {
        var mylist = new List<MyListType>();

        if (_merchantItemes == null)
        {
            return new MultiSelectList(mylist, "ID", "Name");
        }

        if (!_merchantItemes.Any())
        {
            return new MultiSelectList(mylist, "ID", "Name");
        }

        switch (defaultDdItem)
        {
            case DefaultDDItems.ALL:
                mylist.Add(new MyListType()
                {
                    Name = "All",
                    ID = "0"
                });
                break;

            case DefaultDDItems.All_Selected:
                mylist.Add(new MyListType()
                {
                    Name = "All",
                    ID = String.Join(",", _merchantItemes)
                });
                break;
        }

        mylist.AddRange(_merchantItemes.Select(i => new MyListType()
        {
            Name = i.MerchantName,
            ID = i.ID
        }).ToList());

        return new MultiSelectList(mylist, "ID", "Name", _selectedIds);
    }

    public List<SelectListItem> GetSingleSelectDropDown()
    {
        return GetSingleSelectDropDown(DefaultDDItems.NoDefault);
    }

    public List<SelectListItem> GetSingleSelectDropDown(DefaultDDItems defaultDdItem)
    {
        var mylist = new List<SelectListItem>();

        if (_merchantItemes == null)
        {
            return new List<SelectListItem>(0);
        }

        if (!_merchantItemes.Any())
        {
            return new List<SelectListItem>(0);
        }

        switch (defaultDdItem)
        {
            case DefaultDDItems.ALL:
                mylist.Add(new SelectListItem()
                {
                    Value = "All",
                    Text = "0"
                });
                break;

            case DefaultDDItems.All_Selected:
                mylist.Add(new SelectListItem()
                {
                    Value = "All",
                    Text = String.Join(",", _merchantItemes)
                });
                break;
        }

        mylist.AddRange(_merchantItemes.Select(i => new SelectListItem()
        {
            Text = i.MerchantName,
            Value = i.ID,
            Selected = _selectedIds.FirstOrDefault() == i.ID
        }).ToList());

        return mylist;
    }
}

public class CustomerLevelDropDown : IDropDown
{

    private List<CustomerItem> _customerItemes;
    private string[] _selectedIds;


    public CustomerLevelDropDown()
    {
    }

    public CustomerLevelDropDown(List<CustomerItem> items)
    {
        this._customerItemes = items;
    }

    public CustomerLevelDropDown(List<CustomerItem> items, string[] defaultSelectedItems)
    {
        this._customerItemes = items;
        this._selectedIds = defaultSelectedItems;
    }


    public MultiSelectList GetMultiSelectDropDown()
    {
        return GetMultiSelectDropDown(DefaultDDItems.NoDefault);
    }

    public MultiSelectList GetMultiSelectDropDown(DefaultDDItems defaultDdItem)
    {
        var mylist = new List<MyListType>();

        if (_customerItemes == null)
        {
            return new MultiSelectList(mylist, "ID", "Name");
        }

        if (!_customerItemes.Any())
        {
            return new MultiSelectList(mylist, "ID", "Name");
        }

        switch (defaultDdItem)
        {
            case DefaultDDItems.ALL:
                mylist.Add(new MyListType()
                {
                    Name = "All",
                    ID = "0"
                });
                break;

            case DefaultDDItems.All_Selected:
                mylist.Add(new MyListType()
                {
                    Name = "All",
                    ID = String.Join(",", _customerItemes)
                });
                break;
        }

        mylist.AddRange(_customerItemes.Select(i => new MyListType()
        {
            Name = i.CustomerName,
            ID = i.ID
        }).ToList());

        return new MultiSelectList(mylist, "ID", "Name", _selectedIds);
    }

    public List<SelectListItem> GetSingleSelectDropDown()
    {
        return GetSingleSelectDropDown(DefaultDDItems.NoDefault);
    }

    public List<SelectListItem> GetSingleSelectDropDown(DefaultDDItems defaultDdItem)
    {
        var mylist = new List<SelectListItem>();

        if (_customerItemes == null)
        {
            return new List<SelectListItem>(0);
        }

        if (!_customerItemes.Any())
        {
            return new List<SelectListItem>(0);
        }

        switch (defaultDdItem)
        {
            case DefaultDDItems.ALL:
                mylist.Add(new SelectListItem()
                {
                    Value = "All",
                    Text = "0"
                });
                break;

            case DefaultDDItems.All_Selected:
                mylist.Add(new SelectListItem()
                {
                    Value = "All",
                    Text = String.Join(",", _customerItemes)
                });
                break;
        }

        mylist.AddRange(_customerItemes.Select(i => new SelectListItem()
        {
            Text = i.CustomerName,
            Value = i.ID,
            Selected = _selectedIds.FirstOrDefault() == i.ID
        }).ToList());

        return mylist;
    }
}


public interface IDropdownFactory
{
    AdminLevelDropDown GetMultiSelect_AdminDropDown();
    AdminLevelDropDown GetMultiSelect_AdminDropDown(List<AdminItem> items);
    AdminLevelDropDown GetMultiSelect_AdminDropDown(List<AdminItem> items, string[] selecteditems);

    AdminLevelDropDown GetSingleSelect_AdminDropDown();
    AdminLevelDropDown GetSingleSelect_AdminDropDown(List<AdminItem> items);
    AdminLevelDropDown GetSingleSelect_AdminDropDown(List<AdminItem> items, string[] selectedItems);




    MerchantLevelDropDown GetMultiSelect_MerchantDropDown();
    MerchantLevelDropDown GetMultiSelect_MerchantDropDown(List<MerchantItem> items);
    MerchantLevelDropDown GetMultiSelect_MerchantDropDown(List<MerchantItem> items, string[] selecteditems);

    MerchantLevelDropDown GetSingleSelect_MerchantDropDown();
    MerchantLevelDropDown GetSingleSelect_MerchantDropDown(List<MerchantItem> items);
    MerchantLevelDropDown GetSingleSelect_MerchantDropDown(List<MerchantItem> items, string[] selectedItems);



    CustomerLevelDropDown GetMultiSelect_CustomerDropDown();
    CustomerLevelDropDown GetMultiSelect_CustomerDropDown(List<CustomerItem> items);
    CustomerLevelDropDown GetMultiSelect_CustomerDropDown(List<CustomerItem> items, string[] selecteditems);

    CustomerLevelDropDown GetSingleSelect_CustomerDropDown();
    CustomerLevelDropDown GetSingleSelect_CustomerDropDown(List<CustomerItem> items);
    CustomerLevelDropDown GetSingleSelect_CustomerDropDown(List<CustomerItem> items, string[] selectedItems);

}

public class DropDownFactory : IDropdownFactory
{

    public AdminLevelDropDown GetMultiSelect_AdminDropDown()
    {
        return new AdminLevelDropDown();
    }

    public AdminLevelDropDown GetMultiSelect_AdminDropDown(List<AdminItem> items)
    {
        return new AdminLevelDropDown(items);
    }

    public AdminLevelDropDown GetMultiSelect_AdminDropDown(List<AdminItem> items, string[] selecteditems)
    {
        return new AdminLevelDropDown(items, selecteditems);
    }

    public AdminLevelDropDown GetSingleSelect_AdminDropDown()
    {
        return new AdminLevelDropDown();
    }

    public AdminLevelDropDown GetSingleSelect_AdminDropDown(List<AdminItem> items)
    {
        return new AdminLevelDropDown(items);
    }

    public AdminLevelDropDown GetSingleSelect_AdminDropDown(List<AdminItem> items, string[] selectedItems)
    {
        return new AdminLevelDropDown(items, selectedItems);
    }

    public MerchantLevelDropDown GetMultiSelect_MerchantDropDown()
    {
        return new MerchantLevelDropDown();
    }

    public MerchantLevelDropDown GetMultiSelect_MerchantDropDown(List<MerchantItem> items)
    {
        return new MerchantLevelDropDown(items);
    }

    public MerchantLevelDropDown GetMultiSelect_MerchantDropDown(List<MerchantItem> items, string[] selecteditems)
    {
        return new MerchantLevelDropDown(items, selecteditems);
    }

    public MerchantLevelDropDown GetSingleSelect_MerchantDropDown()
    {
        return new MerchantLevelDropDown();
    }

    public MerchantLevelDropDown GetSingleSelect_MerchantDropDown(List<MerchantItem> items)
    {
        return new MerchantLevelDropDown(items);
    }

    public MerchantLevelDropDown GetSingleSelect_MerchantDropDown(List<MerchantItem> items, string[] selectedItems)
    {
        return new MerchantLevelDropDown(items, selectedItems);
    }

    public CustomerLevelDropDown GetMultiSelect_CustomerDropDown()
    {
        return new CustomerLevelDropDown();
    }

    public CustomerLevelDropDown GetMultiSelect_CustomerDropDown(List<CustomerItem> items)
    {
        return new CustomerLevelDropDown(items);
    }

    public CustomerLevelDropDown GetMultiSelect_CustomerDropDown(List<CustomerItem> items, string[] selecteditems)
    {
        return new CustomerLevelDropDown(items, selecteditems);
    }

    public CustomerLevelDropDown GetSingleSelect_CustomerDropDown()
    {
        return new CustomerLevelDropDown();
    }

    public CustomerLevelDropDown GetSingleSelect_CustomerDropDown(List<CustomerItem> items)
    {
        return new CustomerLevelDropDown(items);
    }

    public CustomerLevelDropDown GetSingleSelect_CustomerDropDown(List<CustomerItem> items, string[] selectedItems)
    {
        return new CustomerLevelDropDown(items, selectedItems);
    }
}
var container = new Container();
container.RegisterType<DropDownFactory>().As<IDropdownFactory>().InstancePerHttpRequest();
public class HomeController : Controller
{
    public IDropdownFactory _dropdownFactory;

    public HomeController(IDropdownFactory dropdownFactory)
    {
        this._dropdownFactory = dropdownFactory;
    }

    public ViewResult GetMultiSelect_AdminDropDown(string[] selectedids)
    {
        var adminItems = _adminRepo.GetAdminItems();

        var adminDD = _dropdownFactory.GetMultiSelect_AdminDropDown(adminItems, selectedids);
        return View(adminDD);

    }

    public ViewResult GetSingleSelect_AdminDropDown(string[] selectedids)
    {
        var adminItems = _adminRepo.GetAdminItems();

        var adminDD = _dropdownFactory.GetSingleSelect_AdminDropDown(adminItems, selectedids);
        return View(adminDD);

    }

    public ViewResult GetMultiSelect_MerchantDropDown(string[] selectedids)
    {
        var merchantItems = _merchantRepo.GetMerchantItems();

        var merchantDD = _dropdownFactory.GetMultiSelect_MerchantDropDown(merchantItems, selectedids);
        return View(merchantDD);
    }

    public ViewResult GetSingleSelect_MerchantDropDown(string[] selectedids)
    {
        var merchantItems = _merchantRepo.GetMerchantItems();

        var merchantDD = _dropdownFactory.GetSingleSelect_MerchantDropDown(merchantItems, selectedids);
        return View(merchantDD);
    }

    public ViewResult GetMultiSelect_CustomerDropDown(string[] selectedids)
    {
        var customerItems = _customerRepo.GetCustomerItems();

        var merchantDD = _dropdownFactory.GetMultiSelect_CustomerDropDown(customerItems, selectedids);
        return View(merchantDD);
    }

    public ViewResult GetSingleSelect_CustomerDropDown(string[] selectedids)
    {
        var customerItems = _customerRepo.GetCustomerItems();

        var merchantDD = _dropdownFactory.GetSingleSelect_CustomerDropDown(customerItems, selectedids);
        return View(merchantDD);
    }

}
Updated code with a real example.
Source Link
jNet
  • 31
  • 4

Example to create multiple types if Cascade Dropdown list of different types ( single select / multiple select items ):

                    public class ProductMyListType
                    {
                        public string ID { get; set; }
                        public string ComplexID { get; set; }
                        public string Name { get; set; }
                        public bool Selected { get; set; }
                    }


                    public interfaceclass IProductDropDownItem
                    {
    int name                   public string Value { get; set; }
    Product GetProduct();                   public string Name { get; set; }
                    }

public class ProductA : IProduct
{
                public ProductA(stringclass pn)AdminItem
                    {
        this.name = pn;
    }
          public string nameID { get; set; }
    public Product GetProduct()
    {
        return new Product();
    public string AdminName { get; set; }
                    }

                    public class ProductBMerchantItem
 : IProduct
                  {
                        public string nameID { get; set; }
                        public ProductB(string pn)
MerchantName { get; set; {}
        this.name = pn;
          }

                    public Productclass GetProduct()CustomerItem
                    {
        return new Product();              public string ID { get; set; }
                        public string CustomerName { get; set; }
                    }

}                    public enum DefaultDDItems
                    {

 public interface IProductManager
{
    ProductA PrepareProductA(string productName);               ALL = 0,

    ProductB PrepareProductB(string productName);                  All_Selected = 1,

}                        NoDefault,

public class ProductManager : IProductManager
{
    ProductA PrepareProductA(string productName)
    {
        return new ProductA(productName);
    }First_Selected,

    ProductB PrepareProductB(string productName)
    {
        return new ProductB(productName);
    }CurrentMonth,

}
                        CurrentYear

                    }

                    public interface IDropDown
                    {
                        MultiSelectList GetMultiSelectDropDown();
                        MultiSelectList GetMultiSelectDropDown(DefaultDDItems defaultDdItem);
                        List<SelectListItem> GetSingleSelectDropDown();
                        List<SelectListItem> GetSingleSelectDropDown(DefaultDDItems defaultDdItem);
                    }

                    public class AdminLevelDropDown : IDropDown
                    {
                        private List<AdminItem> _adminItemes;
                        private string[] _selectedIds;

                        public AdminLevelDropDown()
                        {
                        }

                        public AdminLevelDropDown(List<AdminItem> items)
                        {
                            this._adminItemes = items;
                        }

                        public AdminLevelDropDown(List<AdminItem> items, string[] defaultSelectedItems)
                        {
                            this._adminItemes = items;
                            this._selectedIds = defaultSelectedItems;
                        }

                        public MultiSelectList GetMultiSelectDropDown()
                        {
                            return GetMultiSelectDropDown(DefaultDDItems.NoDefault);
                        }

                        public MultiSelectList GetMultiSelectDropDown(DefaultDDItems defaultDdItem)
                        {
                            var mylist = new List<MyListType>();

                            if (_adminItemes == null)
                            {
                                return new MultiSelectList(mylist, "ID", "Name");
                            }

                            if (!_adminItemes.Any())
                            {
                                return new MultiSelectList(mylist, "ID", "Name");
                            }

                            switch (defaultDdItem)
                            {
                                case DefaultDDItems.ALL:
                                    mylist.Add(new MyListType()
                                        {
                                            Name = "All",
                                            ID = "0"
                                        });
                                    break;

                                case DefaultDDItems.All_Selected:
                                    mylist.Add(new MyListType()
                                        {
                                            Name = "All",
                                            ID = String.Join(",", _adminItemes)
                                        });
                                    break;
                                  // Other switch cases can be implemented here
                            }

                            mylist.AddRange(_adminItemes.Select(i => new MyListType()
                                {
                                    Name = i.AdminName,
                                    ID = i.ID
                                }).ToList());

                            return new MultiSelectList(mylist, "ID", "Name", _selectedIds);

                        }

                        public List<SelectListItem> GetSingleSelectDropDown()
                        {
                            return GetSingleSelectDropDown(DefaultDDItems.NoDefault);
                        }

                        public List<SelectListItem> GetSingleSelectDropDown(DefaultDDItems defaultDdItem)
                        {
                            var mylist = new List<SelectListItem>();

                            if (_adminItemes == null)
                            {
                                return new List<SelectListItem>(0);
                            }

                            if (!_adminItemes.Any())
                            {
                                return new List<SelectListItem>(0);
                            }

                            switch (defaultDdItem)
                            {
                                case DefaultDDItems.ALL:
                                    mylist.Add(new SelectListItem()
                                    {
                                        Value = "All",
                                        Text = "0"
                                    });
                                    break;

                                case DefaultDDItems.All_Selected:
                                    mylist.Add(new SelectListItem()
                                    {
                                        Value = "All",
                                        Text = String.Join(",", _adminItemes)
                                    });
                                    break;
                            }

                            mylist.AddRange(_adminItemes.Select(i => new SelectListItem()
                            {
                                Text = i.AdminName,
                                Value = i.ID,
                                Selected = _selectedIds.FirstOrDefault() == i.ID
                            }).ToList());

                            return mylist;
                        }
                    }

                    public class MerchantLevelDropDown : IDropDown
                    {

                        private List<MerchantItem> _merchantItemes;
                        private string[] _selectedIds;


                        public MerchantLevelDropDown()
                        {
                        }

                        public MerchantLevelDropDown(List<MerchantItem> items)
                        {
                            this._merchantItemes = items;
                        }

                        public MerchantLevelDropDown(List<MerchantItem> items, string[] defaultSelectedItems)
                        {
                            this._merchantItemes = items;
                            this._selectedIds = defaultSelectedItems;
                        }


                        public MultiSelectList GetMultiSelectDropDown()
                        {
                            return GetMultiSelectDropDown(DefaultDDItems.NoDefault);
                        }

                        public MultiSelectList GetMultiSelectDropDown(DefaultDDItems defaultDdItem)
                        {
                            var mylist = new List<MyListType>();

                            if (_merchantItemes == null)
                            {
                                return new MultiSelectList(mylist, "ID", "Name");
                            }

                            if (!_merchantItemes.Any())
                            {
                                return new MultiSelectList(mylist, "ID", "Name");
                            }

                            switch (defaultDdItem)
                            {
                                case DefaultDDItems.ALL:
                                    mylist.Add(new MyListType()
                                    {
                                        Name = "All",
                                        ID = "0"
                                    });
                                    break;

                                case DefaultDDItems.All_Selected:
                                    mylist.Add(new MyListType()
                                    {
                                        Name = "All",
                                        ID = String.Join(",", _merchantItemes)
                                    });
                                    break;
                            }

                            mylist.AddRange(_merchantItemes.Select(i => new MyListType()
                            {
                                Name = i.MerchantName,
                                ID = i.ID
                            }).ToList());

                            return new MultiSelectList(mylist, "ID", "Name", _selectedIds);
                        }

                        public List<SelectListItem> GetSingleSelectDropDown()
                        {
                            return GetSingleSelectDropDown(DefaultDDItems.NoDefault);
                        }

                        public List<SelectListItem> GetSingleSelectDropDown(DefaultDDItems defaultDdItem)
                        {
                            var mylist = new List<SelectListItem>();

                            if (_merchantItemes == null)
                            {
                                return new List<SelectListItem>(0);
                            }

                            if (!_merchantItemes.Any())
                            {
                                return new List<SelectListItem>(0);
                            }

                            switch (defaultDdItem)
                            {
                                case DefaultDDItems.ALL:
                                    mylist.Add(new SelectListItem()
                                    {
                                        Value = "All",
                                        Text = "0"
                                    });
                                    break;

                                case DefaultDDItems.All_Selected:
                                    mylist.Add(new SelectListItem()
                                    {
                                        Value = "All",
                                        Text = String.Join(",", _merchantItemes)
                                    });
                                    break;
                            }

                            mylist.AddRange(_merchantItemes.Select(i => new SelectListItem()
                            {
                                Text = i.MerchantName,
                                Value = i.ID,
                                Selected = _selectedIds.FirstOrDefault() == i.ID
                            }).ToList());

                            return mylist;
                        }
                    }

                    public class CustomerLevelDropDown : IDropDown
                    {

                        private List<CustomerItem> _customerItemes;
                        private string[] _selectedIds;


                        public CustomerLevelDropDown()
                        {
                        }

                        public CustomerLevelDropDown(List<CustomerItem> items)
                        {
                            this._customerItemes = items;
                        }

                        public CustomerLevelDropDown(List<CustomerItem> items, string[] defaultSelectedItems)
                        {
                            this._customerItemes = items;
                            this._selectedIds = defaultSelectedItems;
                        }


                        public MultiSelectList GetMultiSelectDropDown()
                        {
                            return GetMultiSelectDropDown(DefaultDDItems.NoDefault);
                        }

                        public MultiSelectList GetMultiSelectDropDown(DefaultDDItems defaultDdItem)
                        {
                            var mylist = new List<MyListType>();

                            if (_customerItemes == null)
                            {
                                return new MultiSelectList(mylist, "ID", "Name");
                            }

                            if (!_customerItemes.Any())
                            {
                                return new MultiSelectList(mylist, "ID", "Name");
                            }

                            switch (defaultDdItem)
                            {
                                case DefaultDDItems.ALL:
                                    mylist.Add(new MyListType()
                                    {
                                        Name = "All",
                                        ID = "0"
                                    });
                                    break;

                                case DefaultDDItems.All_Selected:
                                    mylist.Add(new MyListType()
                                    {
                                        Name = "All",
                                        ID = String.Join(",", _customerItemes)
                                    });
                                    break;
                            }

                            mylist.AddRange(_customerItemes.Select(i => new MyListType()
                            {
                                Name = i.CustomerName,
                                ID = i.ID
                            }).ToList());

                            return new MultiSelectList(mylist, "ID", "Name", _selectedIds);
                        }

                        public List<SelectListItem> GetSingleSelectDropDown()
                        {
                            return GetSingleSelectDropDown(DefaultDDItems.NoDefault);
                        }

                        public List<SelectListItem> GetSingleSelectDropDown(DefaultDDItems defaultDdItem)
                        {
                            var mylist = new List<SelectListItem>();

                            if (_customerItemes == null)
                            {
                                return new List<SelectListItem>(0);
                            }

                            if (!_customerItemes.Any())
                            {
                                return new List<SelectListItem>(0);
                            }

                            switch (defaultDdItem)
                            {
                                case DefaultDDItems.ALL:
                                    mylist.Add(new SelectListItem()
                                    {
                                        Value = "All",
                                        Text = "0"
                                    });
                                    break;

                                case DefaultDDItems.All_Selected:
                                    mylist.Add(new SelectListItem()
                                    {
                                        Value = "All",
                                        Text = String.Join(",", _customerItemes)
                                    });
                                    break;
                            }

                            mylist.AddRange(_customerItemes.Select(i => new SelectListItem()
                            {
                                Text = i.CustomerName,
                                Value = i.ID,
                                Selected = _selectedIds.FirstOrDefault() == i.ID
                            }).ToList());

                            return mylist;
                        }
                    }


                    public interface IDropdownFactory
                    {
                        AdminLevelDropDown GetMultiSelect_AdminDropDown();
                        AdminLevelDropDown GetMultiSelect_AdminDropDown(List<AdminItem> items);
                        AdminLevelDropDown GetMultiSelect_AdminDropDown(List<AdminItem> items, string[] selecteditems);

                        AdminLevelDropDown GetSingleSelect_AdminDropDown();
                        AdminLevelDropDown GetSingleSelect_AdminDropDown(List<AdminItem> items);
                        AdminLevelDropDown GetSingleSelect_AdminDropDown(List<AdminItem> items, string[] selectedItems);




                        MerchantLevelDropDown GetMultiSelect_MerchantDropDown();
                        MerchantLevelDropDown GetMultiSelect_MerchantDropDown(List<MerchantItem> items);
                        MerchantLevelDropDown GetMultiSelect_MerchantDropDown(List<MerchantItem> items, string[] selecteditems);

                        MerchantLevelDropDown GetSingleSelect_MerchantDropDown();
                        MerchantLevelDropDown GetSingleSelect_MerchantDropDown(List<MerchantItem> items);
                        MerchantLevelDropDown GetSingleSelect_MerchantDropDown(List<MerchantItem> items, string[] selectedItems);



                        CustomerLevelDropDown GetMultiSelect_CustomerDropDown();
                        CustomerLevelDropDown GetMultiSelect_CustomerDropDown(List<CustomerItem> items);
                        CustomerLevelDropDown GetMultiSelect_CustomerDropDown(List<CustomerItem> items, string[] selecteditems);

                        CustomerLevelDropDown GetSingleSelect_CustomerDropDown();
                        CustomerLevelDropDown GetSingleSelect_CustomerDropDown(List<CustomerItem> items);
                        CustomerLevelDropDown GetSingleSelect_CustomerDropDown(List<CustomerItem> items, string[] selectedItems);

                    }

                    public class DropDownFactory : IDropdownFactory
                    {

                        public AdminLevelDropDown GetMultiSelect_AdminDropDown()
                        {
                            return new AdminLevelDropDown();
                        }

                        public AdminLevelDropDown GetMultiSelect_AdminDropDown(List<AdminItem> items)
                        {
                            return new AdminLevelDropDown(items);
                        }

                        public AdminLevelDropDown GetMultiSelect_AdminDropDown(List<AdminItem> items, string[] selecteditems)
                        {
                            return new AdminLevelDropDown(items, selecteditems);
                        }

                        public AdminLevelDropDown GetSingleSelect_AdminDropDown()
                        {
                            return new AdminLevelDropDown();
                        }

                        public AdminLevelDropDown GetSingleSelect_AdminDropDown(List<AdminItem> items)
                        {
                            return new AdminLevelDropDown(items);
                        }

                        public AdminLevelDropDown GetSingleSelect_AdminDropDown(List<AdminItem> items, string[] selectedItems)
                        {
                            return new AdminLevelDropDown(items, selectedItems);
                        }

                        public MerchantLevelDropDown GetMultiSelect_MerchantDropDown()
                        {
                            return new MerchantLevelDropDown();
                        }

                        public MerchantLevelDropDown GetMultiSelect_MerchantDropDown(List<MerchantItem> items)
                        {
                            return new MerchantLevelDropDown(items);
                        }

                        public MerchantLevelDropDown GetMultiSelect_MerchantDropDown(List<MerchantItem> items, string[] selecteditems)
                        {
                            return new MerchantLevelDropDown(items, selecteditems);
                        }

                        public MerchantLevelDropDown GetSingleSelect_MerchantDropDown()
                        {
                            return new MerchantLevelDropDown();
                        }

                        public MerchantLevelDropDown GetSingleSelect_MerchantDropDown(List<MerchantItem> items)
                        {
                            return new MerchantLevelDropDown(items);
                        }

                        public MerchantLevelDropDown GetSingleSelect_MerchantDropDown(List<MerchantItem> items, string[] selectedItems)
                        {
                            return new MerchantLevelDropDown(items, selectedItems);
                        }

                        public CustomerLevelDropDown GetMultiSelect_CustomerDropDown()
                        {
                            return new CustomerLevelDropDown();
                        }

                        public CustomerLevelDropDown GetMultiSelect_CustomerDropDown(List<CustomerItem> items)
                        {
                            return new CustomerLevelDropDown(items);
                        }

                        public CustomerLevelDropDown GetMultiSelect_CustomerDropDown(List<CustomerItem> items, string[] selecteditems)
                        {
                            return new CustomerLevelDropDown(items, selecteditems);
                        }

                        public CustomerLevelDropDown GetSingleSelect_CustomerDropDown()
                        {
                            return new CustomerLevelDropDown();
                        }

                        public CustomerLevelDropDown GetSingleSelect_CustomerDropDown(List<CustomerItem> items)
                        {
                            return new CustomerLevelDropDown(items);
                        }

                        public CustomerLevelDropDown GetSingleSelect_CustomerDropDown(List<CustomerItem> items, string[] selectedItems)
                        {
                            return new CustomerLevelDropDown(items, selectedItems);
                        }
                    }

IoC usingUsing AutoFac:

                            var container = new Container();
                            container.RegisterType<ProductManager>RegisterType<DropDownFactory>().As<IProductManager>As<IDropdownFactory>().InstancePerhttpRequestInstancePerHttpRequest();

                
                public class HomeController : Controller
                {
    private IProductManager _productManager;              public IDropdownFactory _dropdownFactory;

                    public HomeController(IProductManagerIDropdownFactory productManagerdropdownFactory)
                    {
                        this._productManager_dropdownFactory = productManager;dropdownFactory;
                    } 

                    public ViewResult GetProductAGetMultiSelect_AdminDropDown(string[] selectedids)
                    {
                        var modeladminItems = _productManager_adminRepo.PrepareProductAGetAdminItems("webapplication");

                        var adminDD = _dropdownFactory.GetMultiSelect_AdminDropDown(adminItems, selectedids);
                        return View(modeladminDD); 

                    } 

                    public ViewResult GetProductBGetSingleSelect_AdminDropDown(string[] selectedids)
                    {
                        var modeladminItems = _productManager_adminRepo.PrepareProductBGetAdminItems("MOBILE");

                        var adminDD = _dropdownFactory.GetSingleSelect_AdminDropDown(adminItems, selectedids);
                        return View(modeladminDD);

                    }

                    public ViewResult GetMultiSelect_MerchantDropDown(string[] selectedids)
                    {
                        var merchantItems = _merchantRepo.GetMerchantItems();

                        var merchantDD = _dropdownFactory.GetMultiSelect_MerchantDropDown(merchantItems, selectedids);
                        return View(merchantDD);
                    }

                    public ViewResult GetSingleSelect_MerchantDropDown(string[] selectedids)
                    {
                        var merchantItems = _merchantRepo.GetMerchantItems(); 

                        var merchantDD = _dropdownFactory.GetSingleSelect_MerchantDropDown(merchantItems, selectedids);
                        return View(merchantDD);
                    }

                    public ViewResult GetMultiSelect_CustomerDropDown(string[] selectedids)
                    {
                        var customerItems = _customerRepo.GetCustomerItems();

                        var merchantDD = _dropdownFactory.GetMultiSelect_CustomerDropDown(customerItems, selectedids);
                        return View(merchantDD);
                    }

                    public ViewResult GetSingleSelect_CustomerDropDown(string[] selectedids)
                    {
                        var customerItems = _customerRepo.GetCustomerItems();

                        var merchantDD = _dropdownFactory.GetSingleSelect_CustomerDropDown(customerItems, selectedids);
                        return View(merchantDD);
                    }

                }
  public class Product { }


public interface IProduct
{
    int name { get; set; }
    Product GetProduct();
}

public class ProductA : IProduct
{
    public ProductA(string pn)
    {
        this.name = pn;
    }
    public string name { get; set; }
    public Product GetProduct()
    {
        return new Product();
    }
}

public class ProductB : IProduct
{
    public string name { get; set; }
    public ProductB(string pn)
    {
        this.name = pn;
    }

    public Product GetProduct()
    {
        return new Product();
    }

}

 public interface IProductManager
{
    ProductA PrepareProductA(string productName);

    ProductB PrepareProductB(string productName);

}

public class ProductManager : IProductManager
{
    ProductA PrepareProductA(string productName)
    {
        return new ProductA(productName);
    }

    ProductB PrepareProductB(string productName)
    {
        return new ProductB(productName);
    }

}

IoC using AutoFac

var container = new Container();
container.RegisterType<ProductManager>().As<IProductManager>().InstancePerhttpRequest();
public class HomeController : Controller
{
    private IProductManager _productManager;
    HomeController(IProductManager productManager)
    {
        this._productManager = productManager;
    }
    public ViewResult GetProductA()
    {
        var model = _productManager.PrepareProductA("webapplication");
        return View(model);
    }
    public ViewResult GetProductB()
    {
        var model = _productManager.PrepareProductB("MOBILE");
        return View(model);
    }
}

Example to create multiple types if Cascade Dropdown list of different types ( single select / multiple select items ):

                    public class MyListType
                    {
                        public string ID { get; set; }
                        public string ComplexID { get; set; }
                        public string Name { get; set; }
                        public bool Selected { get; set; }
                    }


                    public class DropDownItem
                    {
                        public string Value { get; set; }
                        public string Name { get; set; }
                    }

                    public class AdminItem
                    {
                        public string ID { get; set; }
                        public string AdminName { get; set; }
                    }

                    public class MerchantItem
                    {
                        public string ID { get; set; }
                        public string MerchantName { get; set; }
                    }

                    public class CustomerItem
                    {
                        public string ID { get; set; }
                        public string CustomerName { get; set; }
                    }

                    public enum DefaultDDItems
                    {

                        ALL = 0,

                        All_Selected = 1,

                        NoDefault,

                        First_Selected,

                        CurrentMonth,

                        CurrentYear

                    }

                    public interface IDropDown
                    {
                        MultiSelectList GetMultiSelectDropDown();
                        MultiSelectList GetMultiSelectDropDown(DefaultDDItems defaultDdItem);
                        List<SelectListItem> GetSingleSelectDropDown();
                        List<SelectListItem> GetSingleSelectDropDown(DefaultDDItems defaultDdItem);
                    }

                    public class AdminLevelDropDown : IDropDown
                    {
                        private List<AdminItem> _adminItemes;
                        private string[] _selectedIds;

                        public AdminLevelDropDown()
                        {
                        }

                        public AdminLevelDropDown(List<AdminItem> items)
                        {
                            this._adminItemes = items;
                        }

                        public AdminLevelDropDown(List<AdminItem> items, string[] defaultSelectedItems)
                        {
                            this._adminItemes = items;
                            this._selectedIds = defaultSelectedItems;
                        }

                        public MultiSelectList GetMultiSelectDropDown()
                        {
                            return GetMultiSelectDropDown(DefaultDDItems.NoDefault);
                        }

                        public MultiSelectList GetMultiSelectDropDown(DefaultDDItems defaultDdItem)
                        {
                            var mylist = new List<MyListType>();

                            if (_adminItemes == null)
                            {
                                return new MultiSelectList(mylist, "ID", "Name");
                            }

                            if (!_adminItemes.Any())
                            {
                                return new MultiSelectList(mylist, "ID", "Name");
                            }

                            switch (defaultDdItem)
                            {
                                case DefaultDDItems.ALL:
                                    mylist.Add(new MyListType()
                                        {
                                            Name = "All",
                                            ID = "0"
                                        });
                                    break;

                                case DefaultDDItems.All_Selected:
                                    mylist.Add(new MyListType()
                                        {
                                            Name = "All",
                                            ID = String.Join(",", _adminItemes)
                                        });
                                    break;
                                  // Other switch cases can be implemented here
                            }

                            mylist.AddRange(_adminItemes.Select(i => new MyListType()
                                {
                                    Name = i.AdminName,
                                    ID = i.ID
                                }).ToList());

                            return new MultiSelectList(mylist, "ID", "Name", _selectedIds);

                        }

                        public List<SelectListItem> GetSingleSelectDropDown()
                        {
                            return GetSingleSelectDropDown(DefaultDDItems.NoDefault);
                        }

                        public List<SelectListItem> GetSingleSelectDropDown(DefaultDDItems defaultDdItem)
                        {
                            var mylist = new List<SelectListItem>();

                            if (_adminItemes == null)
                            {
                                return new List<SelectListItem>(0);
                            }

                            if (!_adminItemes.Any())
                            {
                                return new List<SelectListItem>(0);
                            }

                            switch (defaultDdItem)
                            {
                                case DefaultDDItems.ALL:
                                    mylist.Add(new SelectListItem()
                                    {
                                        Value = "All",
                                        Text = "0"
                                    });
                                    break;

                                case DefaultDDItems.All_Selected:
                                    mylist.Add(new SelectListItem()
                                    {
                                        Value = "All",
                                        Text = String.Join(",", _adminItemes)
                                    });
                                    break;
                            }

                            mylist.AddRange(_adminItemes.Select(i => new SelectListItem()
                            {
                                Text = i.AdminName,
                                Value = i.ID,
                                Selected = _selectedIds.FirstOrDefault() == i.ID
                            }).ToList());

                            return mylist;
                        }
                    }

                    public class MerchantLevelDropDown : IDropDown
                    {

                        private List<MerchantItem> _merchantItemes;
                        private string[] _selectedIds;


                        public MerchantLevelDropDown()
                        {
                        }

                        public MerchantLevelDropDown(List<MerchantItem> items)
                        {
                            this._merchantItemes = items;
                        }

                        public MerchantLevelDropDown(List<MerchantItem> items, string[] defaultSelectedItems)
                        {
                            this._merchantItemes = items;
                            this._selectedIds = defaultSelectedItems;
                        }


                        public MultiSelectList GetMultiSelectDropDown()
                        {
                            return GetMultiSelectDropDown(DefaultDDItems.NoDefault);
                        }

                        public MultiSelectList GetMultiSelectDropDown(DefaultDDItems defaultDdItem)
                        {
                            var mylist = new List<MyListType>();

                            if (_merchantItemes == null)
                            {
                                return new MultiSelectList(mylist, "ID", "Name");
                            }

                            if (!_merchantItemes.Any())
                            {
                                return new MultiSelectList(mylist, "ID", "Name");
                            }

                            switch (defaultDdItem)
                            {
                                case DefaultDDItems.ALL:
                                    mylist.Add(new MyListType()
                                    {
                                        Name = "All",
                                        ID = "0"
                                    });
                                    break;

                                case DefaultDDItems.All_Selected:
                                    mylist.Add(new MyListType()
                                    {
                                        Name = "All",
                                        ID = String.Join(",", _merchantItemes)
                                    });
                                    break;
                            }

                            mylist.AddRange(_merchantItemes.Select(i => new MyListType()
                            {
                                Name = i.MerchantName,
                                ID = i.ID
                            }).ToList());

                            return new MultiSelectList(mylist, "ID", "Name", _selectedIds);
                        }

                        public List<SelectListItem> GetSingleSelectDropDown()
                        {
                            return GetSingleSelectDropDown(DefaultDDItems.NoDefault);
                        }

                        public List<SelectListItem> GetSingleSelectDropDown(DefaultDDItems defaultDdItem)
                        {
                            var mylist = new List<SelectListItem>();

                            if (_merchantItemes == null)
                            {
                                return new List<SelectListItem>(0);
                            }

                            if (!_merchantItemes.Any())
                            {
                                return new List<SelectListItem>(0);
                            }

                            switch (defaultDdItem)
                            {
                                case DefaultDDItems.ALL:
                                    mylist.Add(new SelectListItem()
                                    {
                                        Value = "All",
                                        Text = "0"
                                    });
                                    break;

                                case DefaultDDItems.All_Selected:
                                    mylist.Add(new SelectListItem()
                                    {
                                        Value = "All",
                                        Text = String.Join(",", _merchantItemes)
                                    });
                                    break;
                            }

                            mylist.AddRange(_merchantItemes.Select(i => new SelectListItem()
                            {
                                Text = i.MerchantName,
                                Value = i.ID,
                                Selected = _selectedIds.FirstOrDefault() == i.ID
                            }).ToList());

                            return mylist;
                        }
                    }

                    public class CustomerLevelDropDown : IDropDown
                    {

                        private List<CustomerItem> _customerItemes;
                        private string[] _selectedIds;


                        public CustomerLevelDropDown()
                        {
                        }

                        public CustomerLevelDropDown(List<CustomerItem> items)
                        {
                            this._customerItemes = items;
                        }

                        public CustomerLevelDropDown(List<CustomerItem> items, string[] defaultSelectedItems)
                        {
                            this._customerItemes = items;
                            this._selectedIds = defaultSelectedItems;
                        }


                        public MultiSelectList GetMultiSelectDropDown()
                        {
                            return GetMultiSelectDropDown(DefaultDDItems.NoDefault);
                        }

                        public MultiSelectList GetMultiSelectDropDown(DefaultDDItems defaultDdItem)
                        {
                            var mylist = new List<MyListType>();

                            if (_customerItemes == null)
                            {
                                return new MultiSelectList(mylist, "ID", "Name");
                            }

                            if (!_customerItemes.Any())
                            {
                                return new MultiSelectList(mylist, "ID", "Name");
                            }

                            switch (defaultDdItem)
                            {
                                case DefaultDDItems.ALL:
                                    mylist.Add(new MyListType()
                                    {
                                        Name = "All",
                                        ID = "0"
                                    });
                                    break;

                                case DefaultDDItems.All_Selected:
                                    mylist.Add(new MyListType()
                                    {
                                        Name = "All",
                                        ID = String.Join(",", _customerItemes)
                                    });
                                    break;
                            }

                            mylist.AddRange(_customerItemes.Select(i => new MyListType()
                            {
                                Name = i.CustomerName,
                                ID = i.ID
                            }).ToList());

                            return new MultiSelectList(mylist, "ID", "Name", _selectedIds);
                        }

                        public List<SelectListItem> GetSingleSelectDropDown()
                        {
                            return GetSingleSelectDropDown(DefaultDDItems.NoDefault);
                        }

                        public List<SelectListItem> GetSingleSelectDropDown(DefaultDDItems defaultDdItem)
                        {
                            var mylist = new List<SelectListItem>();

                            if (_customerItemes == null)
                            {
                                return new List<SelectListItem>(0);
                            }

                            if (!_customerItemes.Any())
                            {
                                return new List<SelectListItem>(0);
                            }

                            switch (defaultDdItem)
                            {
                                case DefaultDDItems.ALL:
                                    mylist.Add(new SelectListItem()
                                    {
                                        Value = "All",
                                        Text = "0"
                                    });
                                    break;

                                case DefaultDDItems.All_Selected:
                                    mylist.Add(new SelectListItem()
                                    {
                                        Value = "All",
                                        Text = String.Join(",", _customerItemes)
                                    });
                                    break;
                            }

                            mylist.AddRange(_customerItemes.Select(i => new SelectListItem()
                            {
                                Text = i.CustomerName,
                                Value = i.ID,
                                Selected = _selectedIds.FirstOrDefault() == i.ID
                            }).ToList());

                            return mylist;
                        }
                    }


                    public interface IDropdownFactory
                    {
                        AdminLevelDropDown GetMultiSelect_AdminDropDown();
                        AdminLevelDropDown GetMultiSelect_AdminDropDown(List<AdminItem> items);
                        AdminLevelDropDown GetMultiSelect_AdminDropDown(List<AdminItem> items, string[] selecteditems);

                        AdminLevelDropDown GetSingleSelect_AdminDropDown();
                        AdminLevelDropDown GetSingleSelect_AdminDropDown(List<AdminItem> items);
                        AdminLevelDropDown GetSingleSelect_AdminDropDown(List<AdminItem> items, string[] selectedItems);




                        MerchantLevelDropDown GetMultiSelect_MerchantDropDown();
                        MerchantLevelDropDown GetMultiSelect_MerchantDropDown(List<MerchantItem> items);
                        MerchantLevelDropDown GetMultiSelect_MerchantDropDown(List<MerchantItem> items, string[] selecteditems);

                        MerchantLevelDropDown GetSingleSelect_MerchantDropDown();
                        MerchantLevelDropDown GetSingleSelect_MerchantDropDown(List<MerchantItem> items);
                        MerchantLevelDropDown GetSingleSelect_MerchantDropDown(List<MerchantItem> items, string[] selectedItems);



                        CustomerLevelDropDown GetMultiSelect_CustomerDropDown();
                        CustomerLevelDropDown GetMultiSelect_CustomerDropDown(List<CustomerItem> items);
                        CustomerLevelDropDown GetMultiSelect_CustomerDropDown(List<CustomerItem> items, string[] selecteditems);

                        CustomerLevelDropDown GetSingleSelect_CustomerDropDown();
                        CustomerLevelDropDown GetSingleSelect_CustomerDropDown(List<CustomerItem> items);
                        CustomerLevelDropDown GetSingleSelect_CustomerDropDown(List<CustomerItem> items, string[] selectedItems);

                    }

                    public class DropDownFactory : IDropdownFactory
                    {

                        public AdminLevelDropDown GetMultiSelect_AdminDropDown()
                        {
                            return new AdminLevelDropDown();
                        }

                        public AdminLevelDropDown GetMultiSelect_AdminDropDown(List<AdminItem> items)
                        {
                            return new AdminLevelDropDown(items);
                        }

                        public AdminLevelDropDown GetMultiSelect_AdminDropDown(List<AdminItem> items, string[] selecteditems)
                        {
                            return new AdminLevelDropDown(items, selecteditems);
                        }

                        public AdminLevelDropDown GetSingleSelect_AdminDropDown()
                        {
                            return new AdminLevelDropDown();
                        }

                        public AdminLevelDropDown GetSingleSelect_AdminDropDown(List<AdminItem> items)
                        {
                            return new AdminLevelDropDown(items);
                        }

                        public AdminLevelDropDown GetSingleSelect_AdminDropDown(List<AdminItem> items, string[] selectedItems)
                        {
                            return new AdminLevelDropDown(items, selectedItems);
                        }

                        public MerchantLevelDropDown GetMultiSelect_MerchantDropDown()
                        {
                            return new MerchantLevelDropDown();
                        }

                        public MerchantLevelDropDown GetMultiSelect_MerchantDropDown(List<MerchantItem> items)
                        {
                            return new MerchantLevelDropDown(items);
                        }

                        public MerchantLevelDropDown GetMultiSelect_MerchantDropDown(List<MerchantItem> items, string[] selecteditems)
                        {
                            return new MerchantLevelDropDown(items, selecteditems);
                        }

                        public MerchantLevelDropDown GetSingleSelect_MerchantDropDown()
                        {
                            return new MerchantLevelDropDown();
                        }

                        public MerchantLevelDropDown GetSingleSelect_MerchantDropDown(List<MerchantItem> items)
                        {
                            return new MerchantLevelDropDown(items);
                        }

                        public MerchantLevelDropDown GetSingleSelect_MerchantDropDown(List<MerchantItem> items, string[] selectedItems)
                        {
                            return new MerchantLevelDropDown(items, selectedItems);
                        }

                        public CustomerLevelDropDown GetMultiSelect_CustomerDropDown()
                        {
                            return new CustomerLevelDropDown();
                        }

                        public CustomerLevelDropDown GetMultiSelect_CustomerDropDown(List<CustomerItem> items)
                        {
                            return new CustomerLevelDropDown(items);
                        }

                        public CustomerLevelDropDown GetMultiSelect_CustomerDropDown(List<CustomerItem> items, string[] selecteditems)
                        {
                            return new CustomerLevelDropDown(items, selecteditems);
                        }

                        public CustomerLevelDropDown GetSingleSelect_CustomerDropDown()
                        {
                            return new CustomerLevelDropDown();
                        }

                        public CustomerLevelDropDown GetSingleSelect_CustomerDropDown(List<CustomerItem> items)
                        {
                            return new CustomerLevelDropDown(items);
                        }

                        public CustomerLevelDropDown GetSingleSelect_CustomerDropDown(List<CustomerItem> items, string[] selectedItems)
                        {
                            return new CustomerLevelDropDown(items, selectedItems);
                        }
                    }

IoC Using AutoFac:

                            var container = new Container();
                            container.RegisterType<DropDownFactory>().As<IDropdownFactory>().InstancePerHttpRequest();

                
                public class HomeController : Controller
                {
                    public IDropdownFactory _dropdownFactory;

                    public HomeController(IDropdownFactory dropdownFactory)
                    {
                        this._dropdownFactory = dropdownFactory;
                    } 

                    public ViewResult GetMultiSelect_AdminDropDown(string[] selectedids)
                    {
                        var adminItems = _adminRepo.GetAdminItems();

                        var adminDD = _dropdownFactory.GetMultiSelect_AdminDropDown(adminItems, selectedids);
                        return View(adminDD); 

                    } 

                    public ViewResult GetSingleSelect_AdminDropDown(string[] selectedids)
                    {
                        var adminItems = _adminRepo.GetAdminItems();

                        var adminDD = _dropdownFactory.GetSingleSelect_AdminDropDown(adminItems, selectedids);
                        return View(adminDD);

                    }

                    public ViewResult GetMultiSelect_MerchantDropDown(string[] selectedids)
                    {
                        var merchantItems = _merchantRepo.GetMerchantItems();

                        var merchantDD = _dropdownFactory.GetMultiSelect_MerchantDropDown(merchantItems, selectedids);
                        return View(merchantDD);
                    }

                    public ViewResult GetSingleSelect_MerchantDropDown(string[] selectedids)
                    {
                        var merchantItems = _merchantRepo.GetMerchantItems(); 

                        var merchantDD = _dropdownFactory.GetSingleSelect_MerchantDropDown(merchantItems, selectedids);
                        return View(merchantDD);
                    }

                    public ViewResult GetMultiSelect_CustomerDropDown(string[] selectedids)
                    {
                        var customerItems = _customerRepo.GetCustomerItems();

                        var merchantDD = _dropdownFactory.GetMultiSelect_CustomerDropDown(customerItems, selectedids);
                        return View(merchantDD);
                    }

                    public ViewResult GetSingleSelect_CustomerDropDown(string[] selectedids)
                    {
                        var customerItems = _customerRepo.GetCustomerItems();

                        var merchantDD = _dropdownFactory.GetSingleSelect_CustomerDropDown(customerItems, selectedids);
                        return View(merchantDD);
                    }

                }
Post Closed as "Not suitable for this site" by RobH, RubberDuck, Mast, Simon Forsberg, rolfl
Source Link
jNet
  • 31
  • 4
Loading