Create SelectList

Discussion in 'ASP .Net' started by shapper, Sep 7, 2008.

  1. shapper

    shapper Guest

    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
    shapper, Sep 7, 2008
    #1
    1. Advertising

  2. shapper

    shapper Guest

    On Sep 7, 8:36 pm, "Mark Rae [MVP]" <> wrote:
    > "shapper" <> wrote in message
    >
    > news:...
    >
    > > How can I create the same list but with different name and value for
    > > each item?

    >
    > I don't understand what you mean... How can it be the same list if it has
    > different contents...?
    >
    > --
    > Mark Rae
    > ASP.NET MVPhttp://www.markrae.net


    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
    shapper, Sep 7, 2008
    #2
    1. Advertising

  3. shapper

    shapper Guest

    On Sep 7, 11:36 pm, "Mark Rae [MVP]" <> wrote:
    > "shapper" <> wrote in message
    >
    > news:...
    >
    > >>> How can I create the same list but with different name and value for
    > >>> each item?

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

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

    >
    > > For example:

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

    >
    > Hmm - are you sure you're not talking about a Dictionary<>...?
    >
    > Dictionary<string, string> MyDictionary = new Dictionary<string, string>();
    > MyDictionary.Add("New York", "NY");
    >
    > --
    > Mark Rae
    > ASP.NET MVPhttp://www.markrae.net


    I am using this in an DropDownList. Shouldn't be a SelectList?
    shapper, Sep 8, 2008
    #3
  4. shapper

    shapper Guest

    On Sep 8, 12:34 pm, "Mark Rae [MVP]" <> wrote:
    > "shapper" <> wrote in message
    >
    > news:...
    >
    > >>> Item 1 > Name = "New York", Value = "NY".

    >
    > >> 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...
    >
    > --
    > Mark Rae
    > ASP.NET MVPhttp://www.markrae.net


    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
    shapper, Sep 8, 2008
    #4
  5. shapper

    shapper Guest

    On Sep 8, 12:34 pm, "Mark Rae [MVP]" <> wrote:
    > "shapper" <> wrote in message
    >
    > news:...
    >
    > >>> Item 1 > Name = "New York", Value = "NY".

    >
    > >> 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...
    >
    > --
    > Mark Rae
    > ASP.NET MVPhttp://www.markrae.net


    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
    shapper, Sep 8, 2008
    #5
  6. shapper

    bruce barker Guest

    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)


    "shapper" wrote:

    > 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
    >
    bruce barker, Sep 8, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gianni
    Replies:
    2
    Views:
    848
    Gianni
    Apr 19, 2004
  2. Sonu
    Replies:
    1
    Views:
    792
  3. rodchar

    asp.net mvc SelectList help

    rodchar, Sep 30, 2009, in forum: ASP .Net
    Replies:
    4
    Views:
    1,809
    rodchar
    Sep 30, 2009
  4. jwcooper

    Mechanize SelectList Issue

    jwcooper, Mar 20, 2007, in forum: Ruby
    Replies:
    1
    Views:
    126
    jwcooper
    Mar 21, 2007
  5. Giel Raijmakers

    popup after selection from selectlist

    Giel Raijmakers, Jan 4, 2008, in forum: Ruby
    Replies:
    0
    Views:
    84
    Giel Raijmakers
    Jan 4, 2008
Loading...

Share This Page