下拉列表不会选择

时间:2013-03-27 12:45:28

标签: drop-down-menu

我是新手,可能会遗漏一些明显的东西。 (请参阅下面的代码)Initializer.cs有效,因为运行Create.cshtml时,下拉项“Regular”和“Comm”正确显示。但是,Create.cshtml不起作用:当标量字段正确加载时,下拉字段保持空白。

我花了几个小时审查这个论坛(和其他人)的回复,并尝试了无数的建议,但都无济于事。例如,有几个人说问题是在控制器和下拉列表中使用相同的名称。我做了那个改变,Create.cshtml仍然不起作用。这是代码:

Models.Flight.cs
public virtual ICollection<FlightType> FlightTypes { get; set; }

Models.FlightType.cs
public class FlightType
{
[Key] 
public int FlightTypeID { get; set; }
public string FlightTypeEntry { get; set; }
}

DAL.FlightInitializer.cs
var flighttypes = new List<FlightType>
{
new FlightType { FlightTypeID = 1, FlightTypeEntry = "Regular" },
new FlightType { FlightTypeID = 2, FlightTypeEntry = "Comm" }
};
flighttypes.ForEach(e => context.FlightType.Add(e));
context.SaveChanges();

Controllers.FlightController.cs
// GET: /Flight/Create
public ActionResult Create()
{
ViewBag.FlightTypeVB = new SelectList(db.FlightType, "FlightTypeID", "FlightTypeEntry");
return View();
}

// POST: /Flight/Create
[HttpPost]
public ActionResult Create(Flight flight)
{
if (ModelState.IsValid)
{
db.Flight.Add(flight);
db.SaveChanges();
return RedirectToAction("Index");
}

ViewBag.FlightTypeVB = new SelectList(db.FlightType, "FlightTypeID", "FlightTypeEntry");
return View(flight);

Views.Flight.Create.cshtml
<div class="editor-label">
@Html.LabelFor(model=>model.AircraftModels)
</div>
<div class="editor-field">
@Html.DropDownList("AircraftModelID", (IEnumerable<SelectListItem>)ViewBag.AircraftModelVB)
@Html.ValidationMessageFor(model => model.AircraftModels)
</div>

非常感谢任何建议。

0 个答案:

没有答案