Geeks With Blogs
Doug.Instance Improving the world one post at a time

Consider the following code:

<%= Html.DropDownList("State", new SelectList(new string[] { "","AK","AL","AR","AS","AZ","CA","CO","CT","DC","DE","FL","GA","GU","HI","IA","ID",
                    "IL","IN","KS","KY","LA","MA","MD","ME","MH","MI","MN","MO","MS","MT","NC","ND","NE","NH","NJ","NM","NV","NY",
                    "OH","OK","OR","PA","PR","PW","RI","SC","SD","TN","TX","UT","VA","VI","VT","WA","WI","WV","WY"})) %>

Simple enough, right?  Display a dropdown list with a value for each state.  There is only one problem.  The resulting HTML looks like this:

<select id="State" name="state"><option></option><option>AK</option>...

While this is valid HTML in most browsers, it fails in IE 8 running in compatability mode.  In essence, it is treated like this:

<option value=""></option><option value="">AK</option>...

A simple solution is to force a value like this:

<%= Html.DropDownList("State", new SelectList((new string[] { "","AK","AL","AR","AS","AZ","CA","CO","CT","DC","DE","FL","GA","GU","HI","IA","ID",
                    "IL","IN","KS","KY","LA","MA","MD","ME","MH","MI","MN","MO","MS","MT","NC","ND","NE","NH","NJ","NM","NV","NY",
                    "OH","OK","OR","PA","PR","PW","RI","SC","SD","TN","TX","UT","VA","VI","VT","WA","WI","WV","WY"}).Select(x => new { Text = x, Value = x}), "Value", "Text", null)) %>

You're welcome!

Posted on Tuesday, February 7, 2012 10:08 AM MVC | Back to top


Comments on this post: Problem: DropDownList Always Posts Blank Value in ASP.Net MVC 2

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Doug Lampe | Powered by: GeeksWithBlogs.net