Just read a pretty nice article describing how to perform the equivalent of data binding in MVC 2 with a drop down list at - http://www.nickriggs.com/posts/rendering-and-binding-drop-down-lists-using-asp-net-mvc-2-editorfor/
The only thing that I found myself needing to add was a per instance css class/classes. The Html.EditorFor() method has an additionalViewData parameter that can be used for this to pass the required css class to the underlying partial view control. In this example I'm assuming you've read the article mentioned above and so I won't explain the meaning of the files and their relationship.
As a side note, I also followed the directions found on codingfreak to enable the much nicer syntax highlighting library...and in case you're wanting to add this to your blog as well, I'll pass on my stupidity so hopefully I'll save you the same...the "Preview" provided by blogger.com does not format it correctly, you actually have to post the blog for it to take the proper form.
The first item is to add the @css entry as the second parameter to the EditorFor method.
// Index.aspx<br />...<br /> <div><label id="lblGender">Gender</label><br /> <br /> </div>
...<br />
Now to consume this properly, all you have to do is utilize a mixture of the htmlAttributes anonymous object in the Html.DropDownListFor method along with the value you set in the Html.EditorFor additionViewData anonymous object as shown below.
<br /><%= Html.EditorFor(m => m.GenderList, new { @css = "search_gender_text" }) %> It's not rocket science, but hopefully it might save you a couple of minutes of research and tinkering.
No comments:
Post a Comment