一、创建一个如下的扩展方法 :
public static SelectList ToSelectList<TEnum>(this TEnum enumObj)
{
var values = from TEnum e in Enum.GetValues(typeof(TEnum))
select new { ID = e, Name = e.ToString() };
return new SelectList(values, "Id", "Name", enumObj);
}
二、控制器里代码如下(其中POStatus是枚举类型):
public virtual ActionResult Edit(Guid? Id)
{
var item = _Purchase_BLL.Get(Id);
ViewData["status"] = ((POStatus)item.Status).ToSelectList();
return View(item);
}
三、前台代码如下所示:
<th width="15%" align="right">
<%: Html.LabelFor(model => model.Status)%>
</th>
<td width="35%">
<%: Html.DropDownListFor(model => model.Status, ViewData["status"] as SelectList)%>
<%: Html.ValidationMessageFor(model => model.Status)%>
</td>
------解决方案--------------------------------------------------------
LZ 是分享吗?