如何编写一个简单的Html.DropDownListFor()?

时间:2022-12-16 03:24:51

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

在ASP。NET MVC 2,我想写一个简单的下拉列表,它提供静态选项。例如,我想在“红色”、“蓝色”和“绿色”之间提供选择。

6 个解决方案

#1


165  

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

请参阅此MSDN文章,并在堆栈溢出上使用示例。

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

假设您有以下Linq/POCO类:

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

最后,假设你有下面的颜色列表。它们可以来自Linq查询、静态列表等:

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


53  

<%: 
     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


30  

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


28  

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


8  

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


3  

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://*.com/a/1528193/1395101 JaredPar ;

源自代码:主程序员&乔尔·瓦伦德;王引用:https://*.com/a/1528193/139510jaredpar;

Thanks Master Programmer && Joel Wahlund && JaredPar ;

感谢程序员大师和乔尔·华隆德和杰雷德帕;

Good luck friends.

祝你好运朋友。

#1


165  

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

请参阅此MSDN文章,并在堆栈溢出上使用示例。

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

假设您有以下Linq/POCO类:

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

最后,假设你有下面的颜色列表。它们可以来自Linq查询、静态列表等:

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


53  

<%: 
     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


30  

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


28  

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


8  

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


3  

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://*.com/a/1528193/1395101 JaredPar ;

源自代码:主程序员&乔尔·瓦伦德;王引用:https://*.com/a/1528193/139510jaredpar;

Thanks Master Programmer && Joel Wahlund && JaredPar ;

感谢程序员大师和乔尔·华隆德和杰雷德帕;

Good luck friends.

祝你好运朋友。