如何使用Asp.net MVC Json保存记录mutipule表 [英] How to save the record mutipule table using Asp.net MVC Json

查看:132
本文介绍了如何使用Asp.net MVC Json保存记录mutipule表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的最后一个项目创建简单的销售系统.我正在创建销售表格.在屏幕外观下方附加了屏幕截图图像. 销售完成后,我需要将数据和lastinsert ID一起保存到多个表中.如果我单击打印发票按钮.我在销售数据库,销售产品中都有一个表,下面显示了快照图像.我不知道如何将记录保存到具有lastinsert ID的多表中.

i am creating simple sales system for my final year project. i am creating a sales Form. attached the screen shot image below how the form look like. after sales completed i need to save the data into multiple table along with the lastinsert id. if i click print invoice button. i have a tables in the database sales,sales product i shown the shot shotimage below.i don't how to save records into multipule table with lastinsert id.

在此处输入图片描述

销售表

id日期小计

销售产品表

id sales_id product_id价格总计数量

id sales_id product_id price qty total

我尝试过的代码

jQuery

  function addProject() {
                    var table_data = [];
                    $('#product_list tbody tr').each(function (row, tr) {
                        var sub = {
                                     //these records i am going to add into sales table
                            'barcode': $(tr).find('td:eq(1)').text(),
                            'pname': $(tr).find('td:eq(2)').text(),
                            'pro_price': $(tr).find('td:eq(3)').text(),
                            'qty': $(tr).find('td:eq(4)').text(),
                            'total_cost': $(tr).find('td:eq(5)').text(),
                        };
                        table_data.push(sub);
                    });

                    //these records i am going to add into sales 
                    var total = $("#total").val();

                    $.ajax({
                        type: 'POST',
                        url: '/product/Save',
                        dataType: 'JSON',
                        data: {
                            total: $('#total').val(), data: table_data
                        },
                        success: function (data) {
                            console.log(_data);

                            var msg;
                            if (isNew) {
                                msg = "Sales Completed";
                            }

                            last_id = data.last_id
                            window.location.href = "print.php?last_id=" + last_id;

                            $.alert({
                                title: 'Success!',
                                content: msg,
                                type: 'green',
                                boxWidth: '400px',
                                theme: 'light',
                                useBootstrap: false,
                                autoClose: 'ok|2000'
                            });
                            isNew = true;
                        },

                        error: function (xhr, status, error) {
                            alert(xhr);

                        }

                    });
                }

控制器

  [HttpPost]
            public ActionResult Save(sale s)
            {
                bool status = false;
                if (ModelState.IsValid)
                {
                    using (saleEntities3 dc = new saleEntities3())
                    {

                          //Sales table
                            var v = dc.sales.Where(a => a.id == s.id).FirstOrDefault();
                            dc.sales.Add(v);  
                            dc.SaveChanges();
                            v.id = s.id; // lastinsertid

                        //how to add into lastinsertid as a  sales product table as a sales_id colum 

//Sales product table i don't how to add

                           status = true;
                    }
                }
                return new JsonResult { Data = new { status = status } };

            }

saleEntities3

 public partial class saleEntities3 : DbContext
    {
        public saleEntities3()
            : base("name=saleEntities3")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

        public virtual DbSet<product> products { get; set; }
        public virtual DbSet<sale> sales { get; set; }
        public virtual DbSet<sales_product> sales_product { get; set; }
    }
}

推荐答案

要保存在Sales_Product表中,您需要根据表结构保存保存的销售ID.

To save in the Sales_Product table you need to save with the id of the saved sales according to your table structure

[HttpPost]
public ActionResult Save(sale s)
{
    bool status = false;
    if (ModelState.IsValid)
    {
        using (saleEntities3 dc = new saleEntities3())
        {

            //Sales table
            var v = dc.sales.Where(a => a.id == s.id).FirstOrDefault();
            dc.sales.Add(v);  
            dc.SaveChanges();

            dc.sales_product.Add(new sales_product{
                 sales_id = s.id,
                 product_id = s.barcode, //I believe this is the product id
                 price = s.pro_price,
                 quantity = s.qty,
                 total = s.total_cost
            });

            dc.SaveChanges();

            status = true;
        }
    }
    return new JsonResult { Data = new { status = status } };

}

这篇关于如何使用Asp.net MVC Json保存记录mutipule表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆