views:

1311

answers:

3

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.

+2  A: 

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.

Tim Scott
didn't check this qtion out for a while. I'll have a look at the lib. Thx
borisCallens
+1  A: 

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.

argibson
what other helpers lack in your opinion. I feel that with the latest drop they are quite what I expected (save for dropdown)
borisCallens
+2  A: 

Hi Boris,

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".

GONeale
I create the selectlist in my controller rather then in my view. Maybe indeed, this is what I'm doing wrong.
borisCallens
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 :(
borisCallens
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.
Simon_Weaver