0

This code worked (correctly reflected the user's current timezone) back in the aspx version.

<%= Html.DropDownList("User.TimeZone", AppHelper.GetUSTimeZones(Model.TimeZone))%>

In switching to Razor I'm finding that the rendered control does not place the user's timezone as the selected item.

I've reviewed this question and see that others have experienced the same issue. Shouldn't I expect HtmlHelpers to work the same when moving to Razor?

Community
  • 1
  • 1
justSteve
  • 5,444
  • 19
  • 72
  • 137

1 Answers1

1

For the overload of DropDownList you are using, the method takes a string for the field name and an IEnumerable of SelectListItem.

http://msdn.microsoft.com/en-us/library/system.web.mvc.html.selectextensions.dropdownlist.aspx

So your AppHelper.GetUSTimeZones(Model.TimeZone)) needs to return IEnumerable<SelectListItem>.

To make an option selected you need to indicate that the SelectListItem is the selected one. So something like:

_timeZoneRepo.RetrieveAll().Select(t => new SelectListItem { Text = t.Name, Value = t.Id, Selected = TimeZone.Id == t.Id ? true : false });

Hope it helps.

James Hull
  • 3,669
  • 2
  • 27
  • 36
  • It's more complex than i'd like it to be but I suppose that's the price of tooling up. In the longer run (e.g. then next time i need it) it (better) pay off. thx – justSteve Jan 08 '12 at 19:47