Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to generate a dropdownlist control in asp.net mvc using HTML helpers

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 207
    Comment on it

    Hello All,

    In MVC (Model, View and Controllers), to create a dropdownlist control we use HTML helpers which helps us to to create various HTML and server side controls.

    A dropdownlist is a collection of objects of SelectListItem class in .net which contains various list items that we can select.

    Depending upon our project requirement, we may either hard code the values in code or retrieve them from a database table.

    Generating dropdownliast using hard coded values:

    DropDownList(string name, IEnumerable<SelectListItem> selectList, string optionLabel);
    

    We have following code packet for this :

    @{
        ViewBag.Title = "Home Page";
    }
    
    <div class="jumbotron">
        <h1>MVC DropDownlist Example</h1>
    </div>
    
    <div class="row">
        @HtmlHelper.DropDownList("ddlDepartment", new List<selectlistitem>
        {
            new SelectListItem {Text = "IT", Value="1", Selected=true},
            new SelectListItem {Text = "HR", Value="2"},
            new SelectListItem {Text = "Management", Value="3"}
        }, "--Select Department--")
    </div>
    

 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: