Somebody explain me Html.DropDown and it's dearest friend SelectList
Asked Answered
S

3

5

If you check my earlier questions you may have noticed I just don't get the SelectList and Html.DropDown(). I find it intrigueing that I seem to be the only one in this. So maybe I should try to change my mindset or maybe there are things I don't know that will clear this all up. I really love the whole MVC framework, but SelectList just doesn't want to fit in my head. So here's my list:

SelectList

  • Why can't I set the selected value after instantiation
  • Why can't I set selectedValue by index of items
  • Why is the selectedvalue sometimes a string, sometimes the class I put into it and sometimes a ListItem
  • Why are the items only accesible through GetItems()
  • Why don't the types of selectedItem and the listItems match?
  • Why are the items you put in the list converted to listItem and the selectedItem not?
  • Why can't I get the count of the items without usint the GetItems() method

Html.DropDownList()

  • Why doesn't modelbinding work with it
  • Why is there no behaviour for defaulting selection when there's only one option
  • Why doesn't making an item SelectedValue in the source selectLIst make it the marked item

Before ppl suggest me to write my own:
Since this will be shipped with the MVC product, I would rather have the offical support for a basic controll then to roll my own and have all the troubles that come with it.

Stamm answered 3/12, 2008 at 9:34 Comment(0)
L
3

I feel your pain. Forgive the shameless plug, but you might look into MvcFluentHtml. You can still use SelectList and MultiSelectList, but you have several other choices. Should work fine with binders.

Layout answered 3/12, 2008 at 21:4 Comment(1)
didn't check this qtion out for a while. I'll have a look at the lib. ThxStamm
W
2

Likewise, I have had a number of bugbears with the HTML Helpers but none more so than SelectList. To add to the list of complaints I would like to know why SelectList does not accept (or is not able to handle) generic IEnumerable<T>. Again, you could cludge together your own implementation but IEnumerable<T> would enable quite a lot of handy functionality (such as direct support for LINQ/Entity Framework) and I feel should be provided.

MvcFluentHtml (now part of MvcContrib) looks like a nice addition though.

Woolfolk answered 15/12, 2008 at 22:29 Comment(1)
what other helpers lack in your opinion. I feel that with the latest drop they are quite what I expected (save for dropdown)Stamm
S
2

I understand the majority of your questions, and strangely enough, I have not come across any problems as yet! However I may be using it exactly how the MVC developers intended me too and haven't needed to venture too much outside the box!

But hopefully to help you a little, here is an example of how I use SelectList and the properties I set:

<%= Html.DropDownList("Module.Status", new SelectList(ViewData.Model.Statuses, "ID", "Name", ((int)ViewData.Model.Module.Status)), new {tabindex = 1, title = "Status"}) %>

and in my controller action I use a ModelBinder (you were saying this doesn't work? not sure what problems you're encountering there, mine is going fine.)

public ActionResult Save(int? id, [Bind(Prefix = "Module", 
            Include = "Name,Description,Status")] Module module)

Then status can be accessed, in my case by "module.Status".

Supervision answered 9/1, 2009 at 5:56 Comment(3)
I create the selectlist in my controller rather then in my view. Maybe indeed, this is what I'm doing wrong.Stamm
To avoid cluttered code, I want to keep my view code as clean as possible. I just started from the thought that I should keep all clutter out of my view to keep it as readable as possible. <%Html.DropDownList("Statuses")%> does look much neater then your example code. But it doesn't do the job :(Stamm
The SelectList is view concept - not intended for use in the model. it should therfore not be in the controller. It seems like a generic list, but it really isnt.Fechter

© 2022 - 2024 — McMap. All rights reserved.