Create SelectList

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

Hello,

I am creating a select list as follows:

new SelectList(new[] { "a", "b"})

How can I create the same list but with different name and value for
each item?

Thanks,
Miguel
 
I don't understand what you mean... How can it be the same list if it has
different contents...?

No ...

I just need to create a SelectList where each ListItem has different
Name and Value ...

For example:

Item 1 > Name = "New York", Value = "NY".

Thanks,
Miguel
 
Hmm - are you sure you're not talking about a Dictionary<>...?

Dictionary<string, string> MyDictionary = new Dictionary<string, string>();
MyDictionary.Add("New York", "NY");

I am using this in an DropDownList. Shouldn't be a SelectList?
 
Perhaps we're getting bogged down by nomenclature here... Can you please
clarify precisely which object you are referring to as a "SelectList"...?

Some of your code would be helpful...

Sure.

I am creating in an ASP.NET MVC view a DropDownList:

<%= Html.DropDownList( "", "Cities", new SelectList( new [] { "New
York", "Paris", "London" } ) ) %>

However, I would like each item to have a name different from its
value. For example:
"New York" would display as "New York" but the value would be "NY".

Thanks,
Miguel
 
the SelectList supports any enumerable list. if you want an id and value in
the constructor you specifiy a DataValueField and a DataTextField. as eval
is done, you don't need to a use a data record, any object will do. try:

new SelectList(new[]
{
new (ID = "1" Value = "a"},
new {ID = "2" Value ="b"}
),
"ID","Value);

-- bruce (sqlwork.com)
 
Back
Top