0

代码:

public enum PriorityEnum
{
    High,
    Medium,
    Low
}

IQueryable<PriorityValues> priorityValues = _enumReadService.GetRecords<PriorityValues>();

var creationOptions = new CreationOptions
{
    PriorityValues = priorityValues.ToList()
};

我得到的下拉菜单显示默认为"High"。但它需要"Medium"。如何将下拉显示默认设置为“中”而不是“高”?

更新:添加渲染下拉代码

<b>@Html.Label("Priority:")</b>
@Html.DropDownListFor(x => x.PriorityValueId, Enum.GetValues(typeof (PriorityValueEnum)).OfType<PriorityValueEnum>().ToList().Select(x => new SelectListItem {Text = x.ToString(), Value = ((int) x).ToString(CultureInfo.InvariantCulture)}))
4

1 回答 1

1

您尚未展示如何呈现 DropDown,但如果您使用以下内容:

@Html.DropDownListFor(x => x.Priority, Model.PriorityValues)

您需要做的就是将Priority控制器操作中的属性值设置为所需的结果:

IEnumerable<SelectListItem> priorityValues = ...
model.PriorityValues = priorityValues;
model.Priority = (int)PriorityEnum.Medium;
return View(model);
于 2013-07-11T07:12:15.297 回答