我在我的应用程序中填充下拉列表时遇到困难。我需要Client_Name
出现在下拉列表中,而不是ClientId
.
我收到的错误出现在控制器中,它以绿色突出显示“列表”并说:
警告:检测到无法访问的代码。
对于类似下拉列表的人群,我没有遇到过这个问题。
我在控制器中的代码如下:
//
// GET: /Trip/Create
public ActionResult Create()
{
List<Vehicle> VehicleList = new List<Vehicle>();
// the vehicle registrations
var VehicleQuery1 = from a in db.Vehicles
orderby a.Vehicle_Reg
select a;
VehicleList.AddRange(VehicleQuery1);
ViewBag.VehicleId = new SelectList(VehicleList, "VehicleId", "Vehicle_Reg");
ViewBag.TodaysDate = DateTime.Now;
Trip NewTrip = new Trip();
NewTrip.Date_Departed = DateTime.Now;
return View(NewTrip);
// the client names
List<Client> ClientList = new List<Client>();
var ClientQuery1 = from b in db.Clients
orderby b.Client_Name
select b;
ClientList.AddRange(ClientQuery1);
ViewBag.ClientId = new SelectList(ClientList, "ClientId", "Client_Name");
return View();
}
我也对创建视图进行了更改,代码如下:
<div class="editor-field">
@Html.DropDownList("ClientId", "Please Select");
@Html.ValidationMessageFor(model => model.CleintId)
</div>