我有一个预订应用程序,它接受一个“预订”对象,并且工作正常。我的问题是,如何将其转换为接受多个记录(来自 JSON):
Booking.cs
namespace MvcApplication4.Models
{
public class Booking
{
[Key()]
public long ID { get; set; }
public long? HID { get; set; }
public long RID { get; set; }
public string Occ { get; set; }
public DateTime CI { get; set; }
public DateTime CO { get; set; }
public long? CID { get; set; }
}
}
BookingsContext.cs
namespace MvcApplication4.Models
{
public class BookingsContext : DbContext
{
public BookingsContext() : base("name=BookingsContext")
{
}
public DbSet<Booking> Bookings { get; set; }
}
}
BookingsController.cs
// POST api/Bookings
public HttpResponseMessage PostBooking(Booking booking)
{
if (ModelState.IsValid)
{
// Add the booking
db.Bookings.Add(booking);
db.SaveChanges();
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, booking);
response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = booking.RID }));
return response;
}
}
当前传递的 JSON 是:
var comment = {ID:0, HID: $('#HID').val(), RID:$('#RID').val(), Occ:$('#Occ').val(), CI:$('#CI').val(), CO:$('#CO').val(), CID:$('#CID').val()},{ID:0, HID: $('#HID').val(), RID:$('#RID').val(), Occ:$('#Occ').val(), CI:$('#CI').val(), CO:$('#CO').val(), CID:$('#CID').val()};
如何将多条记录传递给控制器,这样我就不必多次调用 JSON Post 方法?