【发布时间】:2012-06-10 04:22:56
【问题描述】:
我有一个 DropDownList,它在呈现视图时包含正确的项目和值,但选定的值没有保存在指定的实体字段 Garage 中。目前在创建或编辑帖子方法中保存和返回的值都是 0(无)。我确定这很简单,但我无法弄清楚......提前致谢!
模型类:
public enum GarageType { None = 0, One = 1, Two = 2, Three = 3, Four = 4 }
public int Garage { get; set; }
[NotMapped]
public GarageType GarageEnumValue
{
get { return (GarageType)Garage; }
set{ Garage = (int)value; }
}
Control Create 和 Edit 方法都如下所示:
var statuses = from Property.GarageType s in Enum.GetValues(typeof(Property.GarageType))
select new { ID = (int)s, Name = s.ToString() };
ViewBag.GarageId = new SelectList(statuses, "ID", "Name", statuses.FirstOrDefault().ID);
最后的观点:
@Html.DropDownList("GarageId", String.Empty)
【问题讨论】: