创建MVC与SelectListItem一个通用存储库下拉 [英] Create a generic repository DropDown with SelectListItem in MVC
问题描述
说完我增加了我现在最好的回答下面几个月后回到这个问题。
Having returned to this problem after a few months I've added my current best answer below.
在原来的问题,我仍然在寻找一种简单的方式来实现一个通用的下拉,但标题被更紧密地联系在一起的特定错误,然后我面临。
In the original question I was still looking for a simple way to achieve a generic DropDown but the title was more closely tied to the specific error I was then facing.
我已经修改了标题,以更密切地反映了答案。希望这可以帮助别人。
I've amended the title to reflect the answer more closely. Hopefully this might help someone.
原题:
我试图创建一个下拉使用从如下解禁后的想法列表中的通用模板:
I'm trying to create a generic template for a drop down list using ideas lifted from the follwing post:
<一个href=\"http://stackoverflow.com/questions/14219335/move-html-dropdownlistfor-into-editortemplate\">Move HTML DropDownListFor进入编辑模板
我已经创建了一个DropDownHelper类:
I've created a DropDownHelper class:
public class DDLOptions<T>
{
public T Value { get; set; }
public IEnumerable<SelectListItem> Items { get; set; }
}
我已经从这个修正的控制器:
I've amended the Controller from this:
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
类的工作。
推荐答案
有一种方法可以创建一个我从在计算器上几个指针错位一起普通的下拉列表和这篇文章的 $ C $的CProject 。这是否遵循最佳做法的评论将AP preciated。
There is a way to create a generic DropDown which I've mangled together from a few pointers on StackOverflow and this article on CodeProject. Comments on whether this follows best practice would be appreciated.
我用既有AddList和编辑清单,以便基于HTML类属性的编辑清单和一些jQuery选定的项目。通用编辑清单的建立如下:
I use both an AddList and an EditList to allow for a selected item on the EditList and some jQuery based on html class attributes. The generic EditList is created as follows:
我有与通用的模式,然后我返回实体的视图模型适用于任何下拉列表视图模型。注解是在一个验证的文件中。
I have a viewmodel for any DropDown that fits with the generic pattern and then a ViewModel for the entity I'm returning. Annotations are held in a validation file.
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; }
}
编辑模板
在视图模型指向一个编辑模板UIHint注解。我使用 chosen.js 我的查找,因此HTML属性。
Editor Template
The UIHint annotation on the ViewModel points to an Editor Template. I'm using chosen.js on my lookups, hence the html attributes.
@model WhatWorks.ViewModels.DropDownViewModel
@Html.DropDownList("", Model.Items, new { @class = "chosen chosenLookup" })
控制器
控制器查询当前实体获得了编辑清单所选择的字符串。下拉从 GetEditList&LT调用; O&gt;在库
功能。该视图模型,然后通过映射 Automapper ( GetUpdate(ID)
)
Controller
The controller queries the current entity to get the selected string for the EditList. The DropDown is called from the GetEditList<O>
function in the repository. The ViewModel is then mapped via Automapper (GetUpdate(id)
).
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);
}
库
通用下拉库方法从呼叫类型℃的
(的IEnumerable
组数据; O&GT; tEntityType
在这种情况下)。在收益
方法都转换成字符串,并检查所选择的项目。
Repository
The generic DropDown repository method gets an IEnumerable
set of data from the calling type <O>
(tEntityType
in this case). The return
method converts everything into strings and checks for the selected item.
//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)
});
}
查看
最后,视图通过标准呈现下拉 Html.Editor
这从 UIHint
上验证文件的注释。
View
Finally, the view renders the DropDown via a standard Html.Editor
which picks up the Editor Template from the UIHint
annotation on the validation file.
这篇关于创建MVC与SelectListItem一个通用存储库下拉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!