答案 0 :(得分:-1)
我认为应该用json-jquery完成
var data=[{customerid:101,customername:'name',
detaile:[
{customerid:101,productid:1,product:'service1',desc:'any...',quantity:1,subtotal:100},
{customerid:101,productid:2,product:'service2',desc:'note ',quantity:2,subtotal:200}
]
}];
ajax request :
$.ajax({
url:'/urlcontroller/addDetail',
type: "POST",
data: data,
success: function (data, textStatus, jqXHR) {
alert('success');
}
, error: function (xhr, status, error) {
alert('error');
}
});
和Action中的参数应为类似的类:
public class customer
{
public int customerid { get; set; }
public string customername { get; set; }
public customerdetail[] detaile { get; set; }
}
public class customerdetail
{
public int customerid { get; set; }
public int productid { get; set; }
public string product { get; set; }
public string desc { get; set; }
public int quantity { get; set; }
public int subtotal { get; set; }
}
动作结果:
public jsonresult addDetail(customer records)
{
var db = new StorageContext();
db.customer.Add(records);
db.SaveChanges();
foreach (var item in records.detaile)
{
db.customerdetail.Add(item);
db.SaveChanges();
}
}