在ASP.NET MVC3中我认为它必须是一个已知的问题,而我还没有找到正确答案来解决我的问题。
我有一个像这样的枚举
public enum CardType
{
[Description( "" )]
None = 0,
Visa = 1,
Mastercard = 2,
Discover = 3,
[Description( "American Express" )]
AmericanExpress = 4
}
现在,正确的术语将是从数据库中的表中提取的类似内容,因为我只获得了我现在启用的某些卡类型。我们只有Visa / Mastercard
<select name="CardType" id="CardType">
<option value="1">Visa</option>
<option value="2">Mastercard</option>
</select>
现在假设我想要Mastercard被默认选中。我必须做这样的事情
CardTypes = new List<SelectListItem>();
var ctypes = db.ExecuteReaderDynamic( "CardType_GetAll", null, System.Data.CommandType.StoredProcedure )
.Select( d =>
new SelectListItem
{
Text = d.Name,
Value = d.ID.ToString()
Selected = (d.ID == (int)CardType)
} );
CardTypes.AddRange( ctypes );
现在我对这部分没有任何问题,这部分并不总是没有用。有时它会通过“Mastercard”而不是传递的数字值......我对如何解决这个问题很困惑...只有少数几个我不想使用的解决方案,如果我可以帮助它,因为它只是意味着更多的工作...
答案 0 :(得分:0)
我对你正在做的事感到困惑,我真的不明白。
Selected = (d.ID == (int)CardType)
您是否获得了ID并根据Enum CardType进行检查?这不会产生以下错误:
CardType' is a 'type' but is used like a 'variable'
不应该这样吗?
CardTypes = new List<SelectListItem>();
var ctypes = db.ExecuteReaderDynamic( "CardType_GetAll", null, System.Data.CommandType.StoredProcedure )
.Select( d =>
new SelectListItem
{
Text = d.Name,
Value = d.ID.ToString()
Selected = false
} );
CardTypes.AddRange( ctypes );
SelectList oCards = new SelectList(oCardTypes, (int)CardType.Mastercard);
将oCards直接用于你的组合框?