MVC 从数据库中填充与特定 userId 相关的项目的 dropDownList

MVC Populate from a database a dropDownList of items related to particular userId

我在 Visual Studio 2013 和 C# 中使用 MVC4...

我不知道如何根据用户 table 中的 userId 从航班 table 中的列 'flightDate' 中填充项目的选择性列表。

目前我的设置是这样的..

预订控制器

public ActionResult EditBooking()
{
    ViewBag.BookingList = new SelectList(db.Bookings.All(t => t.UserId == userId).ToString(), "BookingId", "FlightDate");
    return View();
}

编辑预订视图

@using (Html.BeginForm("EditBooking", "Booking", FormMethod.Post))
{
@model project.Models.Booking
<div class="editor-label">
<label>What is the date of your booking?</label>
</div>
    <div class="editor-field">
    @Html.DropDownListFor(model => model.BookingId, (SelectList)ViewBag.BookingList)
</div>
}

有人知道如何根据这些 table 数据生成此条件结果列表吗? 谢谢!

除了 Where() 之后的 ToString() 应该是 ToList():

,你的所有代码看起来都很好
db.Bookings.Where(t=> t.UserId == userId).ToList()