简体   繁体   中英

Insert Master Detail in MVC5

I am developing an Inventory Control System in MVC5 i need help, how to insert/update master and detail data on one click?

Like below openERP picture

在此处输入图片说明

I think it should be done with 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');
            }
        });

and parameters in Action shoud be a class like:

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; }
}

action result:

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM