在MVC5中插入主细节

时间:2015-12-09 16:59:01

标签: c# sql linq linq-to-sql asp.net-mvc-5

我正在开发MVC5中的库存控制系统我需要帮助,如何在一次点击时插入/更新主数据和详细数据?

如下面的openERP图片

enter image description here

1 个答案:

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

}