【发布时间】:2013-06-28 00:46:49
【问题描述】:
我正在玩弄 ASP.NET WebAPI,到目前为止,我已经完成了一些 CRUD 操作。但是我的 Create 操作没有完全正常工作。
通过以下 POST 请求 (JSON),我可以创建一个用户。
{Username:"Bob", FirstName:"Foo", LastName:"Bar", Password:"123", Headline:"Tuna"}
然而对象User实际上有更多的属性——地址集合。
public class User
{
public int UserId { get; set; }
public string Username { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Password { get; set; }
public string Headline { get; set; }
public virtual ICollection<Address> Addresses { get; set; }
}
public class Address
{
public string Id { get; set; }
public string StreetName { get; set; }
public string Country { get; set; }
public string ZipCode { get; set; }
public int Cycle { get; set; }
public int UserId { get; set; }
public User User { get; set; }
}
... ...
我不确定如何创建具有一个或多个地址的用户,因为我不知道下一个地址 ID 必须是什么以及它需要具有什么相应的 UserId。
UPDATE2:
查看我的答案以获得解决方案。
两次
【问题讨论】:
标签: c# json asp.net-web-api