gpt4 book ai didi

c# - 为什么我的下拉菜单不会默认为给定值?

转载 作者:太空宇宙 更新时间:2023-11-03 22:16:22 25 4
gpt4 key购买 nike

我从一个枚举中创建了一个 SelectList。枚举有一个描述,int 值被设置为我想存储在数据库中的值。

问题是,我在构造时设置的默认值 (BLANK) 没有被使用。

这是我的枚举:

public enum Stage
{
[Description("")]
BLANK = -99,
[Description("No Data")]
NoData = 9999,
[Description("Nil")]
Nil = 0,
[Description("Action")]
SAction = 1,
[Description("Action Plus")]
SActionPlus = 2,
[Description("Full")]
Full = 3
}

我在我的 Controller 中创建它:

private static IEnumerable<SelectListItem> GenerateSenStageList()
{
var values = from Stage e in Enum.GetValues(typeof(Stage))
select new { ID = (int)e, Name = e.ToDescription() };

return new SelectList(values, "Id", "Name", (int)Stage.BLANK);
}

我认为最终参数设置所选项目的地方。我将它分配为 ViewData,并像这样访问它:

 <%= Html.DropDownList("Stage", (IEnumerable<SelectListItem>)ViewData["StageList"])%>

但是,Nil 始终是所选值。

我在这里错过了什么??

谢谢!

最佳答案

最后一个参数确定所选值。但是,您传递的是 Stage 枚举值作为最后一个参数,而列表的实际元素由 ID 值 Stage 值组成。要使其正常工作,您必须将 values 中 Stage 值为 BLANK 的实际对象传递给它。

关于c# - 为什么我的下拉菜单不会默认为给定值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5026633/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com