asp.net mvc中的下拉列表无效

时间:2017-11-06 06:58:40

标签: c# asp.net asp.net-mvc asp.net-mvc-4

我正在使用Ado.net数据模型创建Asp.Net MVC

控制器:

public ActionResult Create()
{
    ViewBag.Airlines = new SelectList(db.Airlines.ToList(), "Id", "Name");
    return View();

}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include="DailyPassengerId,SectorPair,FlightNumber,DepartureDate,Airline,Aircraft,Supply,POB,OccPercent,ETD,ATD,Delay")]DailyPassengerFlow dailypassengerflow)
{
    if (ModelState.IsValid)
    {
        db.DailyPassengerFlows.Add(dailypassengerflow);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    return View(dailypassengerflow);
}

查看:

@Html.Label("Airline")
    <div class="col-md-10">
        @Html.DropDownList("Airlines",(SelectList)ViewBag.Airlines)
        @Html.ValidationMessageFor(model => model.Airline)

数据填充在下拉列表中,但未选择数据,即我想将Name传递给此对象Dailypassengerflow。我尝试了很多,但无法得到。

1 个答案:

答案 0 :(得分:0)

@Html.LabelFor(model => model.Airline)
<div class="col-md-10">
@Html.DropDownListFor(model => model.Airline,ViewBag.Airlines as IEnumerable<SelectListItem>)
@Html.ValidationMessageFor(model => model.Airline)
</div>