Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Enums 如何在DropDownListFor中显示保存的枚举值_Enums_Asp.net Mvc 5_Dropdownlistfor - Fatal编程技术网

Enums 如何在DropDownListFor中显示保存的枚举值

Enums 如何在DropDownListFor中显示保存的枚举值,enums,asp.net-mvc-5,dropdownlistfor,Enums,Asp.net Mvc 5,Dropdownlistfor,我有一个名为HtmlBlock的模型,它有一个枚举和一个具有该枚举类型的属性 namespace MyDandyWebsite.Models { public enum HtmlBlockType { Full_Width, OneThird_TwoThirds, TwoThirds_OneThird, OneThird_OneThird_OneThird, OneHalf_OneHalf, OneQuarter_ThreeQuarter,

我有一个名为HtmlBlock的模型,它有一个枚举和一个具有该枚举类型的属性

namespace MyDandyWebsite.Models
{
   public enum HtmlBlockType
 {
    Full_Width,
    OneThird_TwoThirds,
    TwoThirds_OneThird,
    OneThird_OneThird_OneThird,
    OneHalf_OneHalf,
    OneQuarter_ThreeQuarter,
    ThreeQuarter_OneQuarter,
    OneQuarter_OneHalf_OneQuarter,
    OneQuarter_OneQuarter_OneQuarter_OneQuarter
}

public class HtmlBlock
{
  ...
    [Required]
    public HtmlBlockType BlockType { get; set; }
  ...
    [NotMapped]
    public List<SelectListItem> DropDownItems { get; set; }
  ...
    //Initialze class
    public HtmlBlock()
    {
        DropDownItems = new List<SelectListItem>();

        DropDownItems.Add((new SelectListItem { Text = "Full-Width", Value = "0" }));

        ... add the rest of the enums that are too long and wrap badly

    }


}


            @Html.DropDownListFor(
                    model => model.BlockType,
                    new SelectList(Model.DropDownItems, 
                    "Value", "Text", Model.BlockType)
                )
名称空间MyDandyWebsite.Models
{
公共枚举HtmlBlockType
{
全宽,
三分之一,三分之二,
三分之二,
三分之一,三分之一,
一半一半,
四分之一,
四分之三,
四分之一,
四分之一
}
公共类HtmlBlock
{
...
[必需]
公共HtmlBlockType块类型{get;set;}
...
[未映射]
公共列表下拉项{get;set;}
...
//初始化类
公共HtmlBlock()
{
DropDownItems=新列表();
添加((新的SelectListItem{Text=“Full Width”,Value=“0”}));
…添加过长且包装不良的其余枚举
}
}
@Html.DropDownListFor(
model=>model.BlockType,
新建选择列表(Model.DropDownItems,
“值”、“文本”、Model.BlockType)
)
选定的块类型将正确保存到数据库中,并在除上面的下拉列表(其中显示枚举列表中的第一项)之外的每个位置正确使用和列出。 如果我将类HtmlBlock.BlockType的数据类型更改为int,则下拉列表将正确显示,但每个简单的displayfor都会显示一个int,而不是我想要的“Two Threed\u OneThird”

我在谷歌看到了很多解决方法,我想问是否有人知道如何让现有代码在下拉框中显示保存的值。根据我所读的,它应该可以工作。

安装MVC 5.1并使用@Html.EnumDropDownListFor