这个问题应该很简单。我正在尝试将视图中下拉列表中的值传递给控制器。我没有收到错误,但它正在为该属性发送空值。请帮忙..
我的代码如下:
控制器:
public ActionResult Create()
{
var list = new []
{
new Room{ RoomID = 1, Building = "FAYARD HALL"},
new Room{ RoomID = 2, Building = "WHATEVER HALL"},
new Room{ RoomID = 3, Building = "TIME SQUARE"},
new Room{ RoomID = 4, Building = "MISSISSIPPI"},
new Room{ RoomID = 5, Building = "NEW YORK"},
};
var selectList = new SelectList(list,"RoomID", "Building");
ViewData["BuildingList"] = selectList;
return View();
}
//
// POST: /Room/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Room room)
{
if (ModelState.IsValid)
{
db.Rooms.Add(room);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(room);
}
我的观点:
<div>
@Html.LabelFor(model => model.Building, "Building")
</div>
<div>
@Html.DropDownList("BuildingList", String.Empty)
@Html.ValidationMessageFor(model => model.Building)
</div>
请帮忙...
谢谢你。