2

実際には、クラスのプロパティを mvc ドロップダウンに表示する必要があります。私はそれらのものを取得するためにリフレクションを使用しています。しかし、今私の問題は、それらをキーと値のペアとして取得して、ドロップダウンリストに表示することです。

以下のコードを使用しています...

public static Dictionary<string,string> SetProperties()
    {
        Type T = Type.GetType("Entity.Data.Contact");
        PropertyInfo[] resultcontactproperties = T.GetProperties();

        ViewContactModel viewobj = new ViewContactModel();
        viewobj.properties = resultcontactproperties;
        Dictionary<string, string> dic = new Dictionary<string, string>();
        return dic;
    }

それでは、それらを辞書に変換して、以下のドロップダウンで取得する方法は...?

  @Html.DropDownListFor(m=>m.properties, new SelectList(Entity.Data.ContactManager.SetProperties(),"",""), "Select a Property")

Well this is my ViewContactModel

public class ViewContactModel
    {

        public List<Entity.Data.Contact> Contacts;
        public int NoOfContacts { get; set; }
        public Paging pagingmodel { get; set; }
        public PropertyInfo[] properties { get; set; }
    }

In the view I'm using this model 
4

1 に答える 1

2

ディクショナリを使用する必要があり、各ドロップダウン項目の名前と値がプロパティ名そのものであると仮定すると、次の行に沿って何かを使用できます。

    public static Dictionary<string, string> GetProperties<T>(params string[] propNames)
    {
        PropertyInfo[] resultcontactproperties  = null;
        if(propNames.Length > 0)
        {
            resultcontactproperties = typeof(T).GetProperties().Where(p => propNames.Contains(p.Name)).ToArray();
        }
        else
        {
            resultcontactproperties = typeof(T).GetProperties();
        }
        var dict = resultcontactproperties.ToDictionary(propInfo => propInfo.Name, propInfo => propInfo.Name);
        return dict;
    }  

 @Html.DropDownListFor(m=>m.properties, new SelectList(
 Entity.Data.ContactManager.GetProperties<Contact>(),"Key","Value"), 
 "Select a Property")
于 2013-10-16T06:03:59.860 に答える