【问题标题】:Insert Master Detail in MVC5在 MVC5 中插入主细节
【发布时间】:2016-03-15 01:37:12
【问题描述】:

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

如下openERP图片

【问题讨论】:

  • 您可能需要编写一个 OnInsert StoredProcedure 来实现级联更新。
  • 感谢您的回复,我不熟悉 StoredProcedure,您是否有任何示例链接可以在 mvc 中执行此操作。有些人建议我使用 jQuery 插入。使用 jQuery 做到这一点是否又好又安全?
  • 这取决于你是在服务器端(C#)还是客户端(jQuery,使用 AJAX)。对于后者,请参阅以下内容作为起点:stackoverflow.com/questions/17929665/…。如果您在 C# 中执行此操作,您应该能够设置要插入的表以自动更新另一个表,例如:REFERENCES ChildTable (xyz) ON INSERT CASCADE。

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


【解决方案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();
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多