简体   繁体   中英

ASP.NET MVC - create drop down list for a model that has composite primary key?

I'm trying to allow for selecting an item from list of items. The problem is that each item has a composite key, and if I use a drop down list, the value for each item is expected to be a string (but I need two strings for the composite key).

Right now, I've hacked it to combine the composite key into one string with a delimiting character in between when setting the list, and then parsing the value expecting the delimiting character when something is selected. Is there a cleaner/better way of achieving this? Obviously my current approach can fail if one of the keys already is using the same character.

Model:

public class CompositeKeyModel
{
    [Key]
    public string Name { get; set; }

    [Key]
    public string Group { get; set; }
}

How about you create a get property to get the Value of the Property to retrive the composite Key

public class CompositeKeyModel
{
   [Key]
   public string Name { get; set; }

   [Key]
   public string Group { get; set; }

   public string CompositeKey {get
   {
      return Name+Group
   }}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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