几个月后回到这个问题,我在下面添加了我当前的最佳答案。
在最初的问题中,我仍在寻找一种实现通用 DropDown 的简单方法,但标题与我当时面临的特定错误更密切相关。
我已经修改了标题以更紧密地反映答案。希望这可以帮助某人。
原始问题:
DropDownListFor 的通用编辑器模板引发无法转换类型错误
我正在尝试使用从以下帖子中提出的想法为下拉列表创建一个通用模板:
将 Html DropDownListFor 移动到编辑器模板中
我创建了一个 DropDownHelper 类:
public class DDLOptions<T>
{
public T Value { get; set; }
public IEnumerable<SelectListItem> Items { get; set; }
}
我已经修改了控制器:
public ActionResult Create()
{
var model = new FilmEditViewModel();
FilmDropDownViewModel films = new FilmDropDownViewModel
{
Items = _repo.GetSelect(),
};
model.filmName = films;
return View(model);
}
...对此:
public ActionResult Create()
{
var model = new FilmEditViewModel();
DDLOptions<FilmDropDownViewModel> films
= new DDLOptions<FilmDropDownViewModel>
{
Items = _repo.GetSelect()
};
model.filmName = films;
return View(model);
}
这引发以下错误:
Cannot implicitly convert type
'BootstrapSupport.DDLOptions<FilmStore.ViewModels.FilmDropDownViewModel>'
to 'FilmStore.ViewModels.FilmDropDownViewModel'
我也很难弄清楚如何修改编辑器模板以使用修改后的DDLOptions
类。