Asp.net mvc 4 从MVC存储库模式中作为参数传递的表名中进行选择
我正在从事一个MVC4项目,该项目使用工作单元和存储库模式。我有大约25个表,它们是下拉列表或多选列表的只读表。随着我们添加新模块,该列表可能会增加。我想知道是否可以实现一个通用存储库,它根据表名或我向函数传递的某个别名返回一个select或multiselect列表 例如 公共类GenericListReopsitory.GetDropDownList 是否可以在存储库模式中实现类似的功能?可以通过通用存储库执行下拉列表和多选列表 我已经说过了,所以我不再重复了 MultiSelect列表使用与空白列表相同的技术,但如果您想在显示当前选择的同时编辑列表(如上面的链接所示),则需要其他方法,我将使用该方法渲染MultiSelect输入框 模型 MultiSelectList有一个ViewModel,我从主ViewModel调用它。验证保存在单独的文件中 多选视图模型 实体视图模型 验证 编辑器模板 [UIHint\u multiEdit]指向以下编辑器模板。类属性用于和框架Asp.net mvc 4 从MVC存储库模式中作为参数传递的表名中进行选择,asp.net-mvc-4,arguments,repository-pattern,multiple-tables,Asp.net Mvc 4,Arguments,Repository Pattern,Multiple Tables,我正在从事一个MVC4项目,该项目使用工作单元和存储库模式。我有大约25个表,它们是下拉列表或多选列表的只读表。随着我们添加新模块,该列表可能会增加。我想知道是否可以实现一个通用存储库,它根据表名或我向函数传递的某个别名返回一个select或multiselect列表 例如 公共类GenericListReopsitory.GetDropDownList 是否可以在存储库模式中实现类似的功能?可以通过通用存储库执行下拉列表和多选列表 我已经说过了,所以我不再重复了 MultiSelect列表使用
@model WhatWorks.ViewModels.MultiSelectViewModel
@Html.DropDownList("", Model.Items, null, new { @class = "chosen input-xxlarge", multiple = "multiple", })
控制器
在控制器中,我调用存储库两次—一次用于控制器所有者实体(在本例中为TintervationType),另一次用于将提供我们的MultiSelectList值给tOutcome的实体。_repo实例引用标准泛型CRUD、Get和GetById方法中的泛型类型参数T。_多实例使用泛型类型参数O来区分存储库中的实体。这可能不是受欢迎的最佳实践建议,但它确实有效
private readonly IAdminRepository<tInterventionType> _repo;
private readonly IAdminRepository<tOutcome> _multi;
public InterventionTypeController(IAdminRepository<tInterventionType> _repo,
IAdminRepository<tOutcome> _multi)
{
this._repo = _repo;
this._multi = _multi;
}
存储库
存储库中的方法与标准Get相同,但泛型类型参数O引用传递的模型
public IEnumerable<O> GetMultiSelectEntity<O>()
where O : class
{
return context.Set<O>().ToList();
}
控制器
要创建父对象的新实例,请插入父对象,然后迭代子对象集合,依次添加每个子对象
[HttpPost]
public ActionResult Create(InterventionTypeAddViewModel model)
{
if (ModelState.IsValid)
{
var a = new tInterventionType();
a.InjectFrom(model);
_repo.Insert(a);
foreach (var outcomeId in model.outcome)
{
tOutcome o = _repo.GetMulti<tOutcome>(outcomeId);
a.tOutcome.Add(o as tOutcome);
}
_repo.Save();
return RedirectToAction("Index");
}
return View(model);
}
编辑现有父对象,并按照创建方法添加当前选定的项
[HttpPost]
public ActionResult Edit(InterventionTypeUpdateViewModel model, int id)
{
if (ModelState.IsValid)
{
var a = _repo.GetById(id);
a.InjectFrom(model);
a.interventionTypeID = id;
_repo.Update(a);
//clear the child collection (outcome on interventionType)
a.tOutcome.Clear();
//Add current selection
foreach (var outcomeId in model.outcomeID)
{
tOutcome o = _repo.GetMulti<tOutcome>(outcomeId);
a.tOutcome.Add(o as tOutcome);
}
_repo.Save();
return RedirectToAction("Index");
}
return View(model);
}
// GET: /InterventionType/Edit/5
public ActionResult Edit(int id = 0)
{
var selectFrom = _multi.Get();
IEnumerable<int> currentSelect = from o in selectFrom
where o.tInterventionType.Any(m => m.interventionTypeID == id)
select o.outcomeID;
MultiSelectViewModel outcome = new MultiSelectViewModel
{
Items = new MultiSelectList(selectFrom, "outcomeID", "outcome", currentSelect)
};
InterventionTypeEditViewModel a = GetUpdate(id);
a.outcomeID = outcome;
if (a == null)
{
return HttpNotFound();
}
return View(a);
}
public IEnumerable<O> GetMultiSelectEntity<O>()
where O : class
{
return context.Set<O>().ToList();
}
public O GetMulti<O>(int id)
where O : class
{
return context.Set<O>().Find(id);
}
[HttpPost]
public ActionResult Create(InterventionTypeAddViewModel model)
{
if (ModelState.IsValid)
{
var a = new tInterventionType();
a.InjectFrom(model);
_repo.Insert(a);
foreach (var outcomeId in model.outcome)
{
tOutcome o = _repo.GetMulti<tOutcome>(outcomeId);
a.tOutcome.Add(o as tOutcome);
}
_repo.Save();
return RedirectToAction("Index");
}
return View(model);
}
[HttpPost]
public ActionResult Edit(InterventionTypeUpdateViewModel model, int id)
{
if (ModelState.IsValid)
{
var a = _repo.GetById(id);
a.InjectFrom(model);
a.interventionTypeID = id;
_repo.Update(a);
//clear the child collection (outcome on interventionType)
a.tOutcome.Clear();
//Add current selection
foreach (var outcomeId in model.outcomeID)
{
tOutcome o = _repo.GetMulti<tOutcome>(outcomeId);
a.tOutcome.Add(o as tOutcome);
}
_repo.Save();
return RedirectToAction("Index");
}
return View(model);
}