如何在 mvc 下拉列表中设置特定项目的样式
How can I style specific item in a mvc dropdownlist
我有以下代码用值填充下拉列表。如果 x.Level == 0,我希望文本以粗体显示。这可以实现还是不可能在 select 列表中具有 html 格式的文本?
selectItems.AddRange(
LoggedInUser.CategoriesUserCanAdminster.Select(x => new SelectListItem2()
{
Text = String.Concat(Enumerable.Repeat("\xA0\xA0", x.Level + 1)) + x.Name,
Value = x.Path,
Selected = Model.Parent == x.Path,
Disabled = !x.CanAdminister || x.Path.StartsWith(Model.Path)
}
));
}
@Html.DropDownList("Parent", selectItems, new { @class = "form-control" })
将 SelectListItem2 改成如下形式
public class SelectListItem2
{
public string Text {get;set;}
public string Value {get;set;}
public bool Selected {get;set;}
public bool Disabled {get;set;}
public bool Level0 {get;set;}
}
然后使用循环,Html如下
<select>
@foreach (SelectListItem2 item in selectItems)
{
if (item.Level0)
{
<option style="background-color:red" selected='@(item.Selected ? "selected" : "")' id="@item.Value">@item.Text</option>
}
else
{
<option id="@item.Value" selected='@(item.Selected ? "selected" : "")'>@item.Text</option>
}
}
</select>
我有以下代码用值填充下拉列表。如果 x.Level == 0,我希望文本以粗体显示。这可以实现还是不可能在 select 列表中具有 html 格式的文本?
selectItems.AddRange(
LoggedInUser.CategoriesUserCanAdminster.Select(x => new SelectListItem2()
{
Text = String.Concat(Enumerable.Repeat("\xA0\xA0", x.Level + 1)) + x.Name,
Value = x.Path,
Selected = Model.Parent == x.Path,
Disabled = !x.CanAdminister || x.Path.StartsWith(Model.Path)
}
));
}
@Html.DropDownList("Parent", selectItems, new { @class = "form-control" })
将 SelectListItem2 改成如下形式
public class SelectListItem2
{
public string Text {get;set;}
public string Value {get;set;}
public bool Selected {get;set;}
public bool Disabled {get;set;}
public bool Level0 {get;set;}
}
然后使用循环,Html如下
<select>
@foreach (SelectListItem2 item in selectItems)
{
if (item.Level0)
{
<option style="background-color:red" selected='@(item.Selected ? "selected" : "")' id="@item.Value">@item.Text</option>
}
else
{
<option id="@item.Value" selected='@(item.Selected ? "selected" : "")'>@item.Text</option>
}
}
</select>