简体   繁体   English

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

[英]Generic method to return DropdownList Partial View in asp.net

I am working on a controller's action which should be able to return a dropdown list. 我正在做一个控制器的动作,它应该能够返回一个下拉列表。 Arguments for that are parent Entity type, Parent entity Id and Child Entity type. 其参数为父实体类型,父实体ID和子实体类型。

 public ActionResult Dropdown<TParent,TChild>(int id) where TParent : class where TChild:class
 { 
     var dropdownList = new DropdownList<TParent,TChild>(id);
     return PartialView();
 }

DropdownList class looks like this. 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
        //    });
        //}

    }
}

I have no idea how to proceed and I am stuck. 我不知道如何进行,但我被困住了。 I cannot find any solution on google as well. 我也无法在Google上找到任何解决方案。 Basically I want to create a class where I will pass the Id of an Parent Entity, the type of parent Entity and type of Child Entity. 基本上,我想创建一个类,在该类中我将传递父实体的ID,父实体的类型和子实体的类型。 I want it to return the dropdown list of child entities. 我希望它返回子实体的下拉列表。

You can introduce interface 您可以介绍界面

public interface IDropdownOption
{
      public int Value {get; set;}
      public int Text {get; set;}
}

Implement this interface on all the entity for which you need to show the drop down list. 在您需要为其显示下拉列表的所有实体上实现此接口。

you can then change the signature of your function like below 然后您可以如下更改函数的签名

/* 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
}));

}
}

I was able to get it by using NavigationProperty name. 我可以通过使用NavigationProperty名称来获取它。 I had to pass parent and child types, parent Id and Navigation Property name. 我必须传递父代和子代类型,父代ID和导航属性名称。 So the calling method will be like 所以调用方法会像

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

Then the DrodownList class is like 然后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
            });
        }
    }
}

This solution works fine but I am still looking for a better approach where you don't have to pass navigation property name. 此解决方案效果很好,但我仍在寻找一种更好的方法,使您不必传递导航属性名称。

parentEntityEntry.Collection(navigationProperty)

Above method accepts both string as a navigation property name and an Expression which can derive navigation property name from the child type. 上面的方法既接受字符串作为导航属性名称,又接受可以从子类型派生导航属性名称的表达式。

Any idea for expression that need be passed ? 任何需要表达的想法?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM