模型:
public partial class BookModel : BaseNopEntityModel,
ILocalizedModel<BookLocalizedModel>
{
public int TranslatorId { get; set; }
public IList<SelectListItem> AvailableTranslators { get; set; }
}
控制器:
public ActionResult Edit(int id)
{
var bookTranslators = _customerService.GetAllTranslators();
foreach (var item in bookTranslators)
{
model.AvailableTranslators.Add(new SelectListItem()
{
Text = item.Nickname,
Value = item.Id.ToString(),
Selected = item.Nickname == model.Translator
});
}
return View(model);
}
Model.AvailableTranslators 确实有一项 selected = true 但未能在视图中将其显示为默认值。我究竟做错了什么 ?
如何将 selectedItem 显示为默认值?
看法:
@model BookModel
@Html.DropDownListFor(model => model.TranslatorId, Model.AvailableTranslators)
更新:我将数据类型 TranslatorId 更改为字符串
public string TranslatorId { get; set; }
现在下拉列表显示正确的默认值,只是不知道为什么?