的selectedItem屬性我有下面的代碼在Repository.cs
來獲取Timezones
MVC選擇列表的不顯示在下拉
public static IEnumerable<SelectListItem> GetTimeZoneList()
{
return TimeZoneInfo.GetSystemTimeZones().Select(x => new SelectListItem()
{
Value = x.Id,
Text = x.DisplayName
});
}
和我AddEditEmployeeViewModel.cs
是如下:
public class AddEditEmployeeViewModel
{
public string TimeZoneID { get; set; }
public SelectList TimeZones { get; set; }
}
和Ajax調用,我填型號如下:
public PartialViewResult GetAddEditEmployee(string id)
{
var model = new AddEditEmployeeViewModel();
model.TimeZones = new SelectList(Repository.GetTimeZoneList(), "Value", "Text");
var employee = (from e in context.tbl_users where e.eid == eid select new { e.fnamae, e.lname, e.account_status, e.preferred_timezone }).FirstOrDefault();
if (employee == null) return PartialView("_AddEditEmployee", model);
model.TimeZoneID = employee.preferred_timezone;
//model.TimeZoneID will have values like India Standard Time, UTC etc.,
//..Other properties
return PartialView("_AddEditEmp", model);
}
夏娃n雖然model.TimeZoneID
與獲取的選擇列表具有匹配的值,但它不會將值項保留爲選定狀態。我可以看到SelectList
中DB的值。截圖以供參考
這裏是view code
@Html.DropDownListFor(m => m.TimeZoneID, Model.TimeZones, "Select a Timezone", new { @class = "selectpicker", data_width = "100%", })
有什麼錯誤我犯在這裏。需要做什麼修改才能從dropdownlist/selectpicker
中選擇該項目?
你在'model.TimeZoneID'有效值返回局部視圖之前? – Shyju
是的,我這樣做,正如上面代碼中提到的那樣.. @Shyju –
你的代碼看起來非常好。這裏是我從你的代碼中創建的一個工作示例https://dotnetfiddle.net/FiIEOf – Shyju