Hriskesh Ashokan
Hriskesh Ashokan

Reputation: 771

Setting the first default value in a drop down list MVC

I want to be able to set the first value in my dropdown list as "Select a poll", and then load all my select items from my viewdata respectively. How would I go about doing that? My block of code right now looks something like this:

     <select name="ddlPolls" id="ddlPolls">
     <% foreach (var item in (string[][])ViewData["returnPolls"])                
       { %> <option value="<%= item[0] %>"><%= item[1] %></option><% } %>

     </select>

This returns a dropdown list of my polls, but I want the first value in that dropdown list to be "Select a poll" so that I can use jquery to perform .change ajax functions to load another dropdown list. Any help is much appreciated!! :D

Upvotes: 0

Views: 1267

Answers (1)

Peyman
Peyman

Reputation: 3138

Just write your code as follow:

 <select name="ddlPolls" id="ddlPolls">
   <option value="0">Select a poll</option>
   <% foreach (var item in (string[][])ViewData["returnPolls"])                
     { %> <option value="<%= item[0] %>"><%= item[1] %></option><% } %>

 </select>

Upvotes: 2

Related Questions