Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Generic DropdownList in MVC

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.10k
    Comment on it

    Hi All,

    We in most of our projects in MVC come across creating dropdown list, that can be tricky sometimes. In this blog you all will get a preview of creating a generic function for creating dropdown for any list.

    The first thing we need to do is to create a helper class in which you will create the below function to get a dropdown list:

    public static IEnumerable<SelectListItem> GetDropDownList<T>(string text, string value, IEnumerable<T> lst, string selected) where T : class
    {
        List<SelectListItem> list = new List<SelectListItem>();
        list.Add(new SelectListItem { Text = "-Please select-", Value = string.Empty });
        IEnumerable<T> result = lst;
        var lisData = (from items in result
                       select items).AsEnumerable().Select(m => new SelectListItem
                       {
                           Text = (string)m.GetType().GetProperty(text).GetValue(m),
                           Value = Convert.ToString(m.GetType().GetProperty(value).GetValue(m)),
                           Selected = (selected != "") ? (Convert.ToString(
                             m.GetType().GetProperty(value).GetValue(m)) ==
                             selected ? true : false) : false,
                       }).ToList();
        list.AddRange(lisData);
        return list;
    }
    

    Now this will be used to create any dropdown across your project. To use this you will simply need to call the above function as follows in your controller:

    var lstsampleDropdown = Helpers.GetDropDownList<Sample1>("Sample2", "Sample3", Sample4, "Sample5");
    ViewBag.SampleDropdown = lstsampleDropdown;
    
    • Sample1 -> Is the class for which you need to create dropdown for.
    • Sample2 -> Is the text you will need to show in the dropdown.
    • Sample3 -> Is the value that will be used (for most of us its the ID).
    • Sample4 -> Is the IEnumerable.
    • Sample5 -> Is the Option you want to make selected. If you pass "" in the last parameter the 0 index value i.e in our case is "-Please select-" will be selected by default. If you want to make any option selected you will just pass the ID as string.

    Now the only thing remaining is to bind this to VIEW

    @Html.DropDownList("Sample", new System.Web.Mvc.SelectList(ViewBag.SampleDropdown, "Value", "Text"), new { @class = "form-control" })
    

    Now create as many dropdowns as you want by adding one line in your controller and one line in your View.

    Happy Coding......

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: