我正在处理一个控制器的动作,它应该能够返回一个下拉列表。其参数是父实体类型,父实体ID和子实体类型。
public ActionResult Dropdown<TParent,TChild>(int id) where TParent : class where TChild:class
{
var dropdownList = new DropdownList<TParent,TChild>(id);
return PartialView();
}
DropdownList类看起来像这样。
public class DropdownList<TParentEntity, TChildEntity> where TParentEntity : EntityObject where TChildEntity : EntityObject
{
public List<SelectListItem> ListItems;
public DropdownList(int parentId)
{
DatabaseEntities db = new DatabaseEntities();
TParentEntity parentEntity = db.Set<TParentEntity>().Find(parentId);
DbEntityEntry parentEntityEntry = db.Entry(parentEntity);
ListItems.Add(new SelectListItem { Value = "-1", Selected = false, Text = "Select " + typeof(TChildEntity).Name });
//foreach (dynamic entity in childrenEntities)
//{
// ListItems.Add(new SelectListItem
// {
// Value = Convert.ToString(entity.Id),
// Text = entity.Name,
// Selected = false
// });
//}
}
}
我不知道如何继续,我被困住了。我也无法在谷歌上找到任何解决方案。基本上我想创建一个类,我将传递父实体的Id,父实体的类型和子实体的类型。我希望它返回子实体的下拉列表。
答案 0 :(得分:0)
您可以介绍界面
public interface IDropdownOption
{
public int Value {get; set;}
public int Text {get; set;}
}
在您需要显示下拉列表的所有实体上实现此接口。
然后您可以更改功能的签名,如下所示
/* Change Constraints on Type Parameters to allow only IDropdownOption */
public class DropdownList<TParentEntity, TChildEntity> where TParentEntity : EntityObject where TChildEntity : EntityObject, IDropdownOption
{
public List<SelectListItem> ListItems;
public DropdownList(int parentId)
{
DatabaseEntities db = new DatabaseEntities();
TParentEntity parentEntity = db.Set<TParentEntity>().Find(parentId);
DbEntityEntry parentEntityEntry = db.Entry(parentEntity);
ListItems.Add(new SelectListItem { Value = "-1", Selected = false, Text = "Select " + typeof(TChildEntity).Name });
/* assuming you can load child entities as list then you just convert it like below */
ListItems.AddRange(childrenEntities.OfType<IDropdownOption>().Select(s => new SelectItem
{
Text = s.Text, Value = s.Value
}));
}
}
答案 1 :(得分:0)
我可以使用 NavigationProperty 名称来获取它。我必须传递父类和子类型,父ID和导航属性名称。所以调用方法就像
var dropdownList = new DropdownList<TParent,TChild>(id, navigationProperty);
然后DrodownList类就像
public class DropdownList<TParent,TChild> where TParent : class
{
public List<SelectListItem> ListItems;
public DropdownList(int id, string navigationProperty)
{
ListItems = new List<SelectListItem>();
DatabaseEntities db = new DatabaseEntities();
TParent parentEntity = db.Set<TParent>().Find(id);
DbEntityEntry<TParent> parentEntityEntry = db.Entry(parentEntity);
DbCollectionEntry<TParent,TChild> navigationPropertyCollection = parentEntityEntry.Collection(navigationProperty).Cast<TParent, TChild>();
ListItems.Add(new SelectListItem { Value = "-1", Selected = false, Text = "Select " + navigationProperty });
foreach (dynamic entity in navigationPropertyCollection.CurrentValue)
{
ListItems.Add(new SelectListItem
{
Value = Convert.ToString(entity.Id),
Text = entity.Name,
Selected = false
});
}
}
}
此解决方案工作正常,但我仍在寻找一种更好的方法,您无需传递导航属性名称。
parentEntityEntry.Collection(navigationProperty)
上面的方法接受字符串作为导航属性名称和可以从子类型派生导航属性名称的表达式。
是否需要传递表达式的想法?