如何設置下拉列表的選定值?當下拉列表綁定時,選定的值/項目應該是保存在數據庫中的最新數據。 這裏是到目前爲止,我已經得到了什麼:Html.DropDownList SelectedValue
控制器:
int userid = int.Parse(UserID);
if (!string.IsNullOrEmpty(UserID))
{
IEnumerable<SelectListItem> address = db.Address
.Where(c => c.UserID == userid)
.OrderByDescending(c => c.ID)
.Select(c => new SelectListItem
{
//make drop down list multi line
// display complete address
Value = c.ID.ToString(),
Text = c.StreetName + ", " + c.City
});
ViewBag.AddressID = address;
}
return View();
控制器:
@Html.DropDownList("AddressID", null, "Select Address", new { @class = "dropdown-toggle col-md-9 form-control" })