Click here to Skip to main content
Click here to Skip to main content

Tagged as

MVC Generic Dropdownlist for Razor

, 14 Nov 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
Building a generic dropdownlist in MVC for razor view


This code is used to build the generic dropdownlist to query the list from DB so that no need to reproduce the code everywhere .

Using the code

Step 1

Write a static method to perform the logic . Here I query from the database using Entity Framework and for the type T, you need to pass the model name (Database -table name), since the Razor HTML helper accepts the type SelectListItem, we must have the same return type

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

Step 2

Then in the Controller action, you can call the static method as below and pass the result to the MVC view using the ViewBag:

var countryList = Helpers.GetDropDownList<Country>("Name", "CountryCode", "SG");
var currencyList = Helpers.GetDropDownList<CurrencyCode>("Name", "Code", "AUD");

ViewBag.CountryCode = countryList;
ViewBag.Currency = currencyList;

Step 3

You can call the ViewBag in the dropdownlist Html Helper as shown below in the MVC Razor View which is tied to the model. 

@model MvcApplication1.Model.Model1
       <td>@Html.LabelFor(model => model.Currency):</td>
       <td>@Html.DropDownList("Currency", ViewData["Currency"] 
         as SelectListItem[])    @Html.ValidationMessageFor(model => model.Currency)</td>
       <td>@Html.LabelFor(model => model.Country):</td>
         ViewData["Country"] as SelectListItem[])
         @Html.ValidationMessageFor(model => model.CountryCode)</td>


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author


United States United States
No Biography provided

Comments and Discussions

QuestionHow do i get the selected item value? Pinmembershashank1711-Mar-15 23:57 
GeneralThanks a lot! PinmemberEric Castellon24-Nov-14 11:02 
QuestionWhat is DB here.. Pinmemberani007_639-Mar-14 2:33 
AnswerRe: What is DB here.. Pinmembertsoh_tan1-Sep-14 17:50 
QuestionHow to use with ADO.NET Entity Data Model .edmx ? Pinmemberanrorathod9-Feb-14 6:52 
QuestionQuestion about Repository PinmemberJorge Lonzo24-Nov-13 6:52 
AnswerRe: Question about Repository PinmemberSumathi125-Nov-13 2:26 
QuestionGood job Pinmemberwicklowwanderer15-Nov-13 7:42 
AnswerRe: Good job PinmemberSumathi120-Nov-13 21:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150414.1 | Last Updated 14 Nov 2013
Article Copyright 2013 by Sumathi1
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid