简体   繁体   English

ASP.NET MVC-为具有复合主键的模型创建下拉列表?

[英]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 您如何创建一个get属性以获取该属性的值以检索组合键

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

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

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

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

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