我需要我的ASP.NET Web Api 2項目幫助,我可以成功發送JSON數據,我想保存Invites和被邀請的成員。ASP.NET Web Api 2代碼第一個JSON數組返回null
帖子已成功保存數據上的LocalDB
我用的Visual Studio 2017年的腳手架,這是我第一次嘗試在.NET環境
{
"InviteID": 6,
"Subject": "sddsdsds",
"DateTime": "54545:55",
"Members": [
{
"MemberID": 8,
"MemberName": "sdds",
"IsAdmin": false,
"MemberNumber": 0,
"PhoneNumber": null,
"InviteID": 6
},
{
"MemberID": 9,
"MemberName": "sdds",
"IsAdmin": false,
"MemberNumber": 0,
"PhoneNumber": null,
"InviteID": 6
},
{
"MemberID": 10,
"MemberName": "sdds",
"IsAdmin": false,
"MemberNumber": 0,
"PhoneNumber": null,
"InviteID": 6
}
]
}
但是當我嘗試檢索邀請,成員數組爲null:
[
{
"InviteID": 2,
"Subject": null,
"DateTime": null,
"Members": null
},
{
"InviteID": 3,
"Subject": null,
"DateTime": null,
"Members": null
},
{
"InviteID": 4,
"Subject": "sddsdsds",
"DateTime": "54545:55",
"Members": null
},
{
"InviteID": 5,
"Subject": "sddsdsds",
"DateTime": "54545:55",
"Members": null
},
{
"InviteID": 6,
"Subject": "sddsdsds",
"DateTime": "54545:55",
"Members": null
}
]
我的邀請模型
public class Invite
{
public int InviteID { get; set; }
public string Subject { get; set; }
public string DateTime { get; set; }
public virtual List<Member> Members { get; set; }
}
我的會員模式
public class Member
{
public int MemberID { get; set; }
public string MemberName { get; set; }
public bool IsAdmin { get; set; }
public int MemberNumber { get; set; }
public string PhoneNumber { get; set; }
public int InviteID { get; set; }
[JsonIgnore]
public virtual Invite Iinvite { get; set; }
}
我控制器
public class InvitesController : ApiController
{
private InviteContext db = new InviteContext();
public InvitesController()
{
db.Configuration.ProxyCreationEnabled = false;
}
// GET: api/Invites
public IEnumerable<Invite> Getinvites()
{
return db.invites.ToList();
}
// GET: api/Invites/5
[ResponseType(typeof(Invite))]
public IHttpActionResult GetInvite(int id)
{
Invite invite = db.invites.Find(id);
if (invite == null)
{
return NotFound();
}
return Ok(invite);
}
// PUT: api/Invites/5
[ResponseType(typeof(void))]
public IHttpActionResult PutInvite(int id, Invite invite)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != invite.InviteID)
{
return BadRequest();
}
db.Entry(invite).State = EntityState.Modified;
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
if (!InviteExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return StatusCode(HttpStatusCode.NoContent);
}
// POST: api/Invites
[ResponseType(typeof(Invite))]
public IHttpActionResult PostInvite(Invite invite)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.invites.Add(invite);
db.SaveChanges();
return CreatedAtRoute("DefaultApi", new { id = invite.InviteID }, invite);
}
// DELETE: api/Invites/5
[ResponseType(typeof(Invite))]
public IHttpActionResult DeleteInvite(int id)
{
Invite invite = db.invites.Find(id);
if (invite == null)
{
return NotFound();
}
db.invites.Remove(invite);
db.SaveChanges();
return Ok(invite);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
private bool InviteExists(int id)
{
return db.invites.Count(e => e.InviteID == id) > 0;
}
}
我的分貝範圍內
public class InviteContext : DbContext
{
public DbSet<Invite> invites { get; set; }
public DbSet<Member> members { get; set; }
}
編輯 **詢問成員表**
[
{
"MemberID": 1,
"MemberName": "sdds",
"IsAdmin": false,
"MemberNumber": 0,
"PhoneNumber": null,
"InviteID": 2
},
{
"MemberID": 2,
"MemberName": "sdds",
"IsAdmin": false,
"MemberNumber": 0,
"PhoneNumber": null,
"InviteID": 2
},
{
"MemberID": 3,
"MemberName": "sdds",
"IsAdmin": false,
"MemberNumber": 0,
"PhoneNumber": null,
"InviteID": 3
},
{
"MemberID": 4,
"MemberName": "sdds",
"IsAdmin": false,
"MemberNumber": 0,
"PhoneNumber": null,
"InviteID": 4
},
{
"MemberID": 5,
"MemberName": "sdds",
"IsAdmin": false,
"MemberNumber": 0,
"PhoneNumber": null,
"InviteID": 5
},
{
"MemberID": 6,
"MemberName": "sdds",
"IsAdmin": false,
"MemberNumber": 0,
"PhoneNumber": null,
"InviteID": 5
},
{
"MemberID": 7,
"MemberName": "sdds",
"IsAdmin": false,
"MemberNumber": 0,
"PhoneNumber": null,
"InviteID": 5
},
{
"MemberID": 8,
"MemberName": "sdds",
"IsAdmin": false,
"MemberNumber": 0,
"PhoneNumber": null,
"InviteID": 6
},
{
"MemberID": 9,
"MemberName": "sdds",
"IsAdmin": false,
"MemberNumber": 0,
"PhoneNumber": null,
"InviteID": 6
},
{
"MemberID": 10,
"MemberName": "sdds",
"IsAdmin": false,
"MemberNumber": 0,
"PhoneNumber": null,
"InviteID": 6
}
]
在DB是他們從邀請的外鍵成員表? – LiverpoolOwen
是的,Members表中有InviteID外鍵,並且InviteID被插入到Members表中,請檢查我是否單獨顯示了成員的數據。 –