Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.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
C# 样式下拉列表i MVC_C#_Css_Dropdownlistfor - Fatal编程技术网

C# 样式下拉列表i MVC

C# 样式下拉列表i MVC,c#,css,dropdownlistfor,C#,Css,Dropdownlistfor,如何设置dropdownlist中每个selectlistitem的样式? 我可以给每个项目一个id并访问css中的项目吗? 我正在尝试更改每个项目的背景颜色 代码如下: @Html.DropDownListFor(model => model.BehanlingsColour, new SelectList( new List<Object>{ new { value = 0 , text =

如何设置dropdownlist中每个selectlistitem的样式? 我可以给每个项目一个id并访问css中的项目吗? 我正在尝试更改每个项目的背景颜色

代码如下:

@Html.DropDownListFor(model => model.BehanlingsColour, new SelectList(
                  new List<Object>{ 
                       new { value = 0 , text = "Red" },
                       new { value = 1 , text = "Blue" },
                       new { value = 2 , text = "Green"},

                    },
                  "value",
                  "text",
                   2))
@Html.DropDownListFor(model=>model.behanlingscolor,新选择列表(
新列表{
新的{value=0,text=“Red”},
新的{value=1,text=“Blue”},
新的{value=2,text=“Green”},
},
“价值”,
“文本”,
2))

Html.DropDownListFor呈现了一个,所以我不知道你为什么说你没有使用标记

CSS不关心服务器端代码,只关心呈现的HTML,因此如果您遇到问题,请始终向我们显示呈现的HTML和CSS

无论如何,要设计元素的样式(请记住,由于CSS中是“替换元素”,所以设计样式的机会有限,但您可以做一些很酷的事情:

您可以覆盖的id=”“属性,以便在CSS中使用元素选择器,如下所示:

 @Html.DropDownListFor(model => model.BehanlingsColour, new SelectList(
                      new List<Object>{ 
                           new { value = 0 , text = "Red" },
                           new { value = 1 , text = "Blue" },
                           new { value = 2 , text = "Green"},

                        },
                      "value",
                      "text",
                       2), new { id = "mySelect" })


        #mySelect { width: 135px; }
//or
 @Html.DropDownListFor(model => model.BehanlingsColour, new SelectList(
                      new List<Object>{ 
                           new { value = 0 , text = "Red" },
                           new { value = 1 , text = "Blue" },
                           new { value = 2 , text = "Green"},

                        },
                      "value",
                      "text",
                       2), new { class = "dropdown" })
@Html.DropDownListFor(model=>model.behanlingscolor,新选择列表(
新列表{
新的{value=0,text=“Red”},
新的{value=1,text=“Blue”},
新的{value=2,text=“Green”},
},
“价值”,
“文本”,
2) ,新的{id=“mySelect”})
#mySelect{width:135px;}
//或
@Html.DropDownListFor(model=>model.behanlingscolor,新选择列表(
新列表{
新的{value=0,text=“Red”},
新的{value=1,text=“Blue”},
新的{value=2,text=“Green”},
},
“价值”,
“文本”,
2) ,新建{class=“dropdown”})

但是,如果您在部分视图中使用此DropDownList for,则id=“”属性在文档中不会是唯一的,这是非法的。请尝试使用不同的选择器来使用上下文,例如后代选择器。

您应该能够使用css伪选择器,例如:

option:nth-child(1) {
    background: red;
}