0

几个月后回到这个问题,我在下面添加了我当前的最佳答案。

在最初的问题中,我仍在寻找一种实现通用 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类。

4

1 回答 1

1

有一种方法可以创建一个通用的 DropDown ,我从 StackOverflow 上的一些指针和CodeProject上的这篇文章中将其拼凑在一起。对这是否遵循最佳实践的评论将不胜感激。

我同时使用 AddList 和 EditList 来允许 EditList 上的选定项目和一些基于 html 类属性的 jQuery。通用 EditList 创建如下:

楷模

我有一个适合通用模式的任何 DropDown 的视图模型,然后是我要返回的实体的视图模型。注释保存在验证文件中。

下拉视图模型

public class DropDownViewModel
{
    public IEnumerable<SelectListItem> Items { get; set; }
}

实体视图模型

public partial class OrganisationEditViewModel
{
    public int entityID { get; set; }
    public string entityName { get; set; }
    public DropDownViewModel entityTypeID { get; set; }
    public string notes { get; set; }
}

验证

[MetadataTypeAttribute(typeof(OrganisationEditViewModelMetaData))]
public partial class OrganisationEditViewModel
{

}

public class OrganisationEditViewModelMetaData
{
    [Key]
    [ScaffoldColumn(false)]
    [HiddenInput(DisplayValue = false)]
    public int entityID { get; set; }

    [Required]
    [Display(Name = "Organisation")]
    public string entityName { get; set; }

    [Required]
    [Display(Name = "Entity Type")]
    [UIHint("_dropDownEdit")]
    public DropDownViewModel entityTypeID { get; set; }

    [Display(Name = "Notes")]
    public string notes { get; set; }

}

编辑器模板

ViewModel 上的 UIHint 注释指向一个编辑器模板。我在查找中使用了 selected.js,因此使用了 html 属性。

@model WhatWorks.ViewModels.DropDownViewModel

@Html.DropDownList("", Model.Items, new { @class = "chosen chosenLookup" })

控制器

控制器查询当前实体以获取 EditList 的选定字符串。DropDown 是从GetEditList<O>存储库中的函数调用的。然后通过Automapper ( GetUpdate(id)) 映射 ViewModel。

public ActionResult Edit(int id = 0)
{
    var query = _repo.GetByID(id);
    string selected = query.tEntityType.entityTypeID.ToString();

    DropDownViewModel entityType = new DropDownViewModel
    {
        Items = _repo.GetEditList<tEntityType>("entityType", "entityTypeID", selected)
    };

    OrganisationEditViewModel a = GetUpdate(id);
    a.entityTypeID = entityType;

    if (a == null)
    {
        return HttpNotFound();
    }
    return View(a);
}

存储库

通用 DropDown 存储库方法从调用类型(在本例中)获取一IEnumerable组数据。该方法将所有内容转换为字符串并检查所选项目。<O>tEntityTypereturn

//generic Edit dropdown
    public IEnumerable<SelectListItem> GetEditList<O>(string text, string value, string selected) 
                                                        where O : class
    {
        IEnumerable<O> result = context.Set<O>();
        var query = from e in result
                    select new
                    {
                        Value = e.GetType().GetProperty(value).GetValue(e, null),
                        Text = e.GetType().GetProperty(text).GetValue(e, null)
                    };

        return query.AsEnumerable()
            .Select(s => new SelectListItem
            {
                Value = s.Value.ToString(),
                Text = s.Text.ToString(),
                Selected = (selected == s.Value.ToString() ? true : false)
            });
    }

看法

最后,视图通过一个标准呈现 DropDown,该标准从验证文件的注释中Html.Editor提取编辑器模板。UIHint

于 2014-05-05T20:02:52.723 回答