C#ASP.NET向单个用户添加属性列表

时间:2018-11-25 15:05:29

标签: c# asp.net asp.net-web-api

嗨,我正在尝试学习如何创建Web ASP.NET应用程序,但我一直坚持尝试获得此结果。

{
  "memberNumber":"1234567890",
  "forename":"Fred",
  "surname":"Smith",
  "products":[
    {
      "name":"Health Ins",
      "cost":100
    },
    {
      "name":"Travel Ins",
      "cost":150
    }
  ]
}

我不知道如何为每个成员添加不同的产品名称/成本。我可以显示memberNumber / forename / surname,但是在将products.add行添加到members.add行时出现错误。

这是我的会员模型:

public class Member
    {
        public int memberNumber { get; set; } = 0;
        public string forename { get; set; } = "";
        public string surname { get; set; } = "";
        public List<Products> products { get; set; }
    }

    public class Products
    {
        public string name { get; set; } = "";
        public int cost { get; set; } = 0;
    }

这是我的控制人:

public class MembersController : ApiController
    {
        List<Member> members = new List<Member>();
        List<Products> products = new List<Products>();

        public MembersController()
        {
            members.Add(new Member { memberNumber = 1234567890, forename = "Fred", surname = "Smith"});
            members.Add(new Member { memberNumber = 1, forename = "Big", surname = "Ben"});
            members.Add(new Member { memberNumber = 2, forename = "Jack", surname = "Ryan" });

            products.Add(new Products { name = "Health Ins", cost = 100 });
            products.Add(new Products { name = "Travel Ins", cost = 150 });
        }
        // GET: api/Members
        public List<Member> Get()
        {
            return members;
        }

        // GET: api/Members/5
        public Member Get(int id)
        {
            return members.Where(x => x.memberNumber == id).FirstOrDefault();
        }
    }

不包括Post / Put / Delete RESTful服务。

2 个答案:

答案 0 :(得分:2)

products.Add(new Products { name = "Health Ins", cost = 100 });
products.Add(new Products { name = "Travel Ins", cost = 150 });

将这些数据添加到

List<Products> products = new List<Products>();

而不是您在Member课堂上的期望。

我认为您可能想在一个成员中添加products集合,而不是另一个List

您可以使用Auto-Property Initializers来初始化products类中的Member列表集合。

public class Member
{
    public int memberNumber { get; set; } = 0;
    public string forename { get; set; } = "";
    public string surname { get; set; } = "";
    public List<Products> products { get; set; } = new List<Products>();
}

MembersController构造的数据将是这样。

public class MembersController : ApiController
{
    List<Member> members = new List<Member>();
    public MembersController()
    {
        var member1 = new Member { memberNumber = 1234567890, forename = "Fred", surname = "Smith"};
        var member2 = new Member { memberNumber = 1, forename = "Big", surname = "Ben"};
        var member3 = new Member { memberNumber = 2, forename = "Jack", surname = "Ryan" };
        member1.products.Add(new Products { name = "Health Ins", cost = 100 });
        member1.products.Add(new Products { name = "Travel Ins", cost = 150 });

        members.Add(member1);
        members.Add(member2);
        members.Add(member3);
    }
    // GET: api/Members
    public List<Member> Get()
    {
        return members;
    }

    // GET: api/Members/5
    public Member Get(int id)
    {
        return members.Where(x => x.memberNumber == id).FirstOrDefault();
    }
}

答案 1 :(得分:1)

您的位置差不多,但是您需要为每个成员分配一个产品列表,如下所示:

  List<Products> p= new List<Products>();
  p.Add(new Products { name = "Health Ins", cost = 100 });
  p.Add(new Products { name = "Travel Ins", cost = 150 });    
  members.Add(new Member { memberNumber = 1234567890, forename = "Fred", surname = "Smith", Products =p});

 // Second member
  p= new List<Products>();
  p.Add(new Products { name = "Big Bens products", cost = 222});        
  members.Add(new Member { memberNumber = 1, forename = "Big", surname = "Ben", Products =p});

因此,您可以添加包含所需产品的成员数量。请注意,我们不是在谈论ArrayList,而是在谈论List。 ArrayLists很久以前已不推荐使用。