gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 21:31:15 26 4
gpt4 key购买 nike

您好,我正在尝试学习如何创建 Web ASP.NET 应用程序,但我一直在努力获得这个结果。

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

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

这是我的成员(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; }
}

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

这是我的 Controller :

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 服务。

最佳答案

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 InitializersMember 类中初始化 products 列表集合。

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();
}
}

关于C# ASP.NET 向单个用户添加属性列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53468810/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com