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
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>
精彩评论