【发布时间】:2017-07-26 17:11:32
【问题描述】:
我正在为现有 Driver(可以从下拉列表中选择)创建一个 Trailer。
@Html.DropDownListFor(x => x.Driver.driverID, (SelectList)ViewBag.DriverID, "-- Please Select -- ", new { @class = "form-control" })
对于 CREATE 功能,它可以完美运行。
//Create Get
public ActionResult Create()
{
ViewBag.DriverID = new SelectList(db.Drivers, "driverID", "driverFullName");
return View();
}
对于 EDIT 功能(编辑预告片编号并保留驱动程序 NULL)它不起作用。
//Edit Get
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Trailer trailer = db.Trailers.Find(id);
if (trailer == null)
{
return HttpNotFound();
}
ViewBag.DriverID = new SelectList(db.Drivers.ToList(), "driverID", "driverFullName");
return View(trailer);
}
我有-- 请在下拉列表中选择-- 作为第一个空值。
我怎样才能从下拉列表中将 NULL 值放在第一个空值上(因此预告片将选择 NO 驱动程序)?
【问题讨论】:
-
请向我们展示在您的控制器中创建和编辑操作以及如何填充 ViewBag.DriverID。
-
为什么要
null? -
如果 --Please Select-- 被选中,这意味着它的 NULL 对吗?为什么你想要 null?
-
--请选择-- 只是一个空槽。我想为拖车选择一个 NULL,没有选择驱动程序。
-
您能提供 Drivers 模型并且您想选择 -- 请选择 -- 将返回 null 吗?但是当它--请选择--时你不能提交对吗?
标签: c# asp.net asp.net-mvc asp.net-mvc-4 razor