Setting Selected Item in FluentHtml Select List

I ran into a small issue today when using the FluentHtml Select() HtmlHelper.  My original code was not properly setting the value of the selected item in the drop down:

 <%=this.Select(m => m.Motorcycle.Make).Selected(Model.Motorcycle.Make).Label("Vehicle Make").Options(Model.MakeList)%>

I kept ending up with no item being selected from the drop down even though I definitely had a vehicle Make populated in my model and my drop down was correctly populated with the collection from the "MakeList" property of the view model.  It turns out that the solution was a simple matter of corrected ordering:

 <%=this.Select(m => m.Motorcycle.Make).Options(Model.MakeList).Selected(Model.Motorcycle.Make).Label("Vehicle Make")%>

In hindsight I guess this order makes a little more sense intuitively anyway.  However, the fluent API did not constrain me from running into this issue.  So, just a heads up, when using the Select() helper, set the Options() first, and then set the selected value by invoking the Selected() helper method.

Tweet Post Share Update Email RSS