在asp.net中返回DropdownList部分视图的通用方法

问题描述 投票:0回答:2

我正在处理一个控制器的动作,它应该能够返回一个下拉列表。其参数是父实体类型,父实体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,父实体的类型和子实体的类型。我希望它返回子实体的下拉列表。

c# asp.net entity-framework asp.net-mvc-4 generics
2个回答
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
}));

}
}

0
投票

我能够通过使用NavigationProperty名称获得它。我必须传递父类和子类型,父ID和导航属性名称。所以调用方法就像

var dropdownList = new DropdownList<TParent,TChild>(id, navigationProperty);

然后下拉列表类就像

 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)

Above方法接受string作为导航属性名称和Expression,它可以从子类型派生导航属性名称。

表达的任何想法都需要通过?

© www.soinside.com 2019 - 2024. All rights reserved.