Drop Down List Value not passing to controller
This question should be very simple.. I am trying to pass values in my
drop down list in my view to the controller.. I'm not getting errors but
it's sending a null value for that property. Please help..
My code is as follows:
Controller:
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);
}
MY VIEW:
<div>
@Html.LabelFor(model => model.Building, "Building")
</div>
<div>
@Html.DropDownList("BuildingList", String.Empty)
@Html.ValidationMessageFor(model => model.Building)
</div>
Please help...
Thank you.
No comments:
Post a Comment