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>
}
- 目前我知道正在正确传递 userId。
- 我不知道我的 SelectList 是否真的生成了一个列表
booking table 行附加了正确的 userId?? - 我的
代码可能有误!
- 我也不知道我是否在视图中创建一个列表,并将 flightDate 作为值,将 bookingId 作为列表的 ID。
有人知道如何根据这些 table 数据生成此条件结果列表吗?
谢谢!
除了 Where()
之后的 ToString()
应该是 ToList()
:
,你的所有代码看起来都很好
db.Bookings.Where(t=> t.UserId == userId).ToList()
我在 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>
}
- 目前我知道正在正确传递 userId。
- 我不知道我的 SelectList 是否真的生成了一个列表 booking table 行附加了正确的 userId?? - 我的 代码可能有误!
- 我也不知道我是否在视图中创建一个列表,并将 flightDate 作为值,将 bookingId 作为列表的 ID。
有人知道如何根据这些 table 数据生成此条件结果列表吗? 谢谢!
除了 Where()
之后的 ToString()
应该是 ToList()
:
db.Bookings.Where(t=> t.UserId == userId).ToList()