How to write a simple Html.DropDownListFor()?

2019-01-01 03:55发布

问题:

In ASP.NET MVC 2, I\'d like to write a very simple dropdown list which gives static options. For example I\'d like to provide choices between \"Red\", \"Blue\", and \"Green\".

回答1:

See this MSDN article and an example usage here on Stack Overflow.

Let\'s say that you have the following Linq/POCO class:

public class Color
{
    public int ColorId { get; set; }
    public string Name { get; set; }
}

And let\'s say that you have the following model:

public class PageModel 
{
   public int MyColorId { get; set; }
}

And, finally, let\'s say that you have the following list of colors. They could come from a Linq query, from a static list, etc.:

public static IEnumerable<Color> Colors = new List<Color> { 
    new Color {
        ColorId = 1,
        Name = \"Red\"
    },
    new Color {
        ColorId = 2,
        Name = \"Blue\"
    }
};

In your view, you can create a drop down list like so:

<%= Html.DropDownListFor(n => n.MyColorId, 
                         new SelectList(Colors, \"ColorId\", \"Name\")) %>


回答2:

<%: 
     Html.DropDownListFor(
           model => model.Color, 
           new SelectList(
                  new List<Object>{ 
                       new { value = 0 , text = \"Red\"  },
                       new { value = 1 , text = \"Blue\" },
                       new { value = 2 , text = \"Green\"}
                    },
                  \"value\",
                  \"text\",
                   Model.Color
           )
        )
%>

or you can write no classes, put something like this directly to the view.



回答3:

Avoid of lot of fat fingering by starting with a Dictionary in the Model

namespace EzPL8.Models
{
    public class MyEggs
    {
        public Dictionary<int, string> Egg { get; set; }

        public MyEggs()
        {
            Egg = new Dictionary<int, string>()
            {
                { 0, \"No Preference\"},
                { 1, \"I hate eggs\"},
                { 2, \"Over Easy\"},
                { 3, \"Sunny Side Up\"},
                { 4, \"Scrambled\"},
                { 5, \"Hard Boiled\"},
                { 6, \"Eggs Benedict\"}
            };

    }


    }

In the View convert it to a list for display

@Html.DropDownListFor(m => m.Egg.Keys,
                         new SelectList(
                             Model.Egg, 
                             \"Key\", 
                             \"Value\"))


回答4:

Hi here is how i did it in one Project :

     @Html.DropDownListFor(model => model.MyOption,                
                  new List<SelectListItem> { 
                       new SelectListItem { Value = \"0\" , Text = \"Option A\" },
                       new SelectListItem { Value = \"1\" , Text = \"Option B\" },
                       new SelectListItem { Value = \"2\" , Text = \"Option C\" }
                    },
                  new { @class=\"myselect\"})

I hope it helps Somebody. Thanks



回答5:

Or if it\'s from a database context you can use

@Html.DropDownListFor(model => model.MyOption, db.MyOptions.Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() }))


回答6:

With \"Please select one Item\"

@Html.DropDownListFor(model => model.ContentManagement_Send_Section,
  new List<SelectListItem> { new SelectListItem { Value = \"0\", Text = \"Plese Select one Item\" } }
    .Concat(db.NameOfPaperSections.Select(x => new SelectListItem { Text = x.NameOfPaperSection, Value = x.PaperSectionID.ToString() })),
  new { @class = \"myselect\" })  

Derived from the codes: Master Programmer && Joel Wahlund ;
King Reference : https://stackoverflow.com/a/1528193/1395101 JaredPar ;

Thanks Master Programmer && Joel Wahlund && JaredPar ;

Good luck friends.



回答7:

@using (Html.BeginForm()) {
    <p>Do you like pizza?
        @Html.DropDownListFor(x => x.likesPizza, new[] {
            new SelectListItem() {Text = \"Yes\", Value = bool.TrueString},
            new SelectListItem() {Text = \"No\", Value = bool.FalseString}
        }, \"Choose an option\") 
    </p>
    <input type = \"submit\" value = \"Submit my answer\" />
} 

I think this answer is similar to Berat\'s, in that you put all the code for your DropDownList directly in the view. But I think this is an efficient way of creating a y/n (boolean) drop down list, so I wanted to share it.

Some notes for beginners:

  • Don\'t worry about what \'x\' is called - it is created here, for the first time, and doesn\'t link to anything else anywhere else in the MVC app, so you can call it what you want - \'x\', \'model\', \'m\' etc.
  • The placeholder that users will see in the dropdown list is \"Choose an option\", so you can change this if you want.
  • There\'s a bit of text preceding the drop down which says \"Do you like pizza?\"
  • This should be complete text for a form, including a submit button, I think

Hope this helps someone,