将 ID 从一个 table 插入到另一个 MVC table (1: N)

Insert ID from one table to another MVC table (1: N)

美好的一天

我是 MVC 和数据库关系的新手。 所以我正在创建一个服务系统(产品订购)我知道必须有一个 1:N(一对多)关系,因为客户可以订购许多产品。 我有 2 个 table,第一个 table 存储请求服务的人的数据和在这种情况下生成的 ID,变量的名称是 REQ_NO,第二个 table 存储客户要求的产品。 我的问题如下,如何将数据库中生成的客户 ID 分配给它要求的产品,例如:如果生成客户,ID 304 要求 2 个产品,这些产品被分配 ID 304

注意:

这是我的代码

Class

    public partial class TB_CS_TEST2
{
    public decimal REQ_NO { get; set; }
    public string REQUESTOR_EMPNO { get; set; }
    public string DEPT_CD { get; set; }
    public string ORG_NAME { get; set; }
    public string EMAIL_ID { get; set; }
    public string APPROVER1 { get; set; }
    public string APPROVER2 { get; set; }
    public Nullable<decimal> TOTAL_AMOUNT { get; set; }
    public Nullable<System.DateTime> REQUEST_DATE { get; set; }
    public Nullable<System.DateTime> APPROVAL_DATE { get; set; }
    public Nullable<System.DateTime> EVENT_DATE { get; set; }
    public string EVENT_PLACE { get; set; }
    public string PURPOSE { get; set; }
    public string REMARKS { get; set; }
    public string STATUS { get; set; }
    public Nullable<System.DateTime> CRT_DATE { get; set; }
    public string EXT { get; set; }

    public virtual TB_CS_TEST TB_CS_TEST { get; set; }
}

public partial class TB_CS_TEST
{
    public decimal REQ_NO { get; set; }
    public Nullable<decimal> SEQ_NO { get; set; }
    public Nullable<decimal> ITEM_ID { get; set; }
    public Nullable<decimal> QUANTITY { get; set; }
    public string UOM { get; set; }
    public Nullable<decimal> UNIT_PRICE { get; set; }
    public Nullable<decimal> EXTENDED_AMT { get; set; }
    public Nullable<System.DateTime> CRT_DATE { get; set; }

    public virtual TB_CS_TEST2 TB_CS_TEST2 { get; set; }
}

控制器

public class DataHeaderController : Controller
{
    //
    // GET: /DataHeader/

    public ActionResult DataHeader()
    {
        List<MvcApplication31.TB_CS_TEST2> hdr = new List<MvcApplication31.TB_CS_TEST2> { new TB_CS_TEST2 { REQ_NO = 0, REQUESTOR_EMPNO = "", DEPT_CD = "", ORG_NAME = "", EMAIL_ID = "", APPROVER1 = "", APPROVER2 = "", TOTAL_AMOUNT = 0, REQUEST_DATE = DateTime.Now, APPROVAL_DATE = DateTime.Now, EVENT_DATE = DateTime.Now, EVENT_PLACE = "", PURPOSE = "", REMARKS = "", STATUS = "", EXT = "" } };
        Entities db = new Entities();
        IEnumerable<SelectListItem> listplace = db.TB_RST_SVC_PLACE.Select(c => new SelectListItem
        {
            Value = c.NAMEPLACE,
            Text = c.NAMEPLACE
        }
        );
        // ViewData["NAMEPLACE"] = new SelectList(db.TB_RST_SVC_PLACE, "NAMEPLACE", "NAMEPLACE");
        ViewData["NAMEPLACE"] = listplace;

        IEnumerable<SelectListItem> listapp = db.TB_RST_SVC_APPROVERS.Select(c => new SelectListItem
        {
            Value = c.EMAIL_APP,
            Text = c.NAME_APP
        }
        );
        ViewData["APPROVER2"] = listapp;

        return View(hdr);
    }
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult DataHeader(List<MvcApplication31.TB_CS_TEST2> hdr)
    {
        Entities db = new Entities();
        IEnumerable<SelectListItem> listplace = db.TB_RST_SVC_PLACE.Select(c => new SelectListItem
        {
            Value = c.NAMEPLACE,
            Text = c.NAMEPLACE
        }
        );
        // ViewData["NAMEPLACE"] = new SelectList(db.TB_RST_SVC_PLACE, "NAMEPLACE", "NAMEPLACE");
        ViewData["NAMEPLACE"] = listplace;

        IEnumerable<SelectListItem> listapp = db.TB_RST_SVC_APPROVERS.Select(c => new SelectListItem
        {
            Value = c.EMAIL_APP,
            Text = c.NAME_APP
        }
        );
        ViewData["APPROVER2"] = listapp;

        if (ModelState.IsValid)
        {
            using (Entities cd = new Entities())
            {
                foreach (var p in hdr)
                {
                    p.CRT_DATE = DateTime.Now;
                    cd.TB_CS_TEST2.Add(p);
                }
                cd.SaveChanges();
                ModelState.Clear();
                hdr = new List<MvcApplication31.TB_CS_TEST2> { new TB_CS_TEST2 { REQ_NO = 0, REQUESTOR_EMPNO = "", DEPT_CD = "", ORG_NAME = "", EMAIL_ID = "", APPROVER1 = "", APPROVER2 = "", TOTAL_AMOUNT = 0, REQUEST_DATE = DateTime.Now, APPROVAL_DATE = DateTime.Now, EVENT_DATE = DateTime.Now, EVENT_PLACE = "", PURPOSE = "", REMARKS = "", STATUS = "", EXT = "" } };
            }
        }
        return View(hdr);
    }
}

public class BulkController : Controller
{
    //
    // GET: /Bulk/

    public ActionResult Bulk()
    {

        //List<MvcApplication31.TB_CS_TEST> ci = new List<MvcApplication31.TB_CS_TEST> { new TB_CS_TEST { REQ = 0, CONTACTNO = "", CONTACTPERSON = "" } };
        List<MvcApplication31.TB_CS_TEST> ci = new List<MvcApplication31.TB_CS_TEST> { new TB_CS_TEST { REQ_NO = 0, SEQ_NO = 0, ITEM_ID = 0, QUANTITY = 0, UOM = "", UNIT_PRICE = 0, EXTENDED_AMT = 0 } };
        Entities db = new Entities();
        IEnumerable<SelectListItem> listplace = db.TB_RST_SVC_PLACE.Select(c => new SelectListItem
        {
            Value = c.NAMEPLACE,
            Text = c.NAMEPLACE
        }
       );
        // ViewData["NAMEPLACE"] = new SelectList(db.TB_RST_SVC_PLACE, "NAMEPLACE", "NAMEPLACE");
        ViewData["NAMEPLACE"] = listplace;

        IEnumerable<SelectListItem> listapp = db.TB_RST_SVC_APPROVERS.Select(c => new SelectListItem
        {
            Value = c.EMAIL_APP,
            Text = c.NAME_APP
        }
        );
        ViewData["APPROVER2"] = listapp;            
        IEnumerable<SelectListItem> listproduct = db.TB_POS_PRODUCTS.Select(c => new SelectListItem
        {
            Value = c.ID.ToString(),
            Text = c.DESCRIPTION
        }
        );
        ViewData["PRODUCTS"] = new SelectList(db.TB_POS_PRODUCTS, "ID", "DESCRIPTION");
        //ViewData["DESCRIPTION"] = listproduct;
        return View(ci);
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Bulk(List<MvcApplication31.TB_CS_TEST> ci)
    {
        Entities db = new Entities();
        IEnumerable<SelectListItem> listplace = db.TB_RST_SVC_PLACE.Select(c => new SelectListItem
        {
            Value = c.NAMEPLACE,
            Text = c.NAMEPLACE
        }
       );
        // ViewData["NAMEPLACE"] = new SelectList(db.TB_RST_SVC_PLACE, "NAMEPLACE", "NAMEPLACE");
        ViewData["NAMEPLACE"] = listplace;

        IEnumerable<SelectListItem> listapp = db.TB_RST_SVC_APPROVERS.Select(c => new SelectListItem
        {
            Value = c.EMAIL_APP,
            Text = c.NAME_APP
        }
        );
        ViewData["APPROVER2"] = listapp;
        IEnumerable<SelectListItem> listproduct = db.TB_POS_PRODUCTS.Select(c => new SelectListItem
        {
            Value = c.ID.ToString(),
            Text = c.DESCRIPTION
        }
        );
        ViewData["PRODUCTS"] = new SelectList(db.TB_POS_PRODUCTS, "ID", "DESCRIPTION");
        //ViewData["DESCRIPTION"] = listproduct;

        if (ModelState.IsValid)
        {
            using (Entities dc = new Entities())
            {

                foreach (var i in ci)
                {
                    i.CRT_DATE = DateTime.Now;
                    dc.TB_CS_TEST.Add(i);
                }
                dc.SaveChanges();
                ViewBag.Message = "Data Successfully saved!";
                ModelState.Clear();
                //ci = new List<MvcApplication31.TB_CS_TEST> { new MvcApplication31.TB_CS_TEST { CONTACTID = 0, CONTACTNO = "", CONTACTPERSON = "" } };
                ci = new List<MvcApplication31.TB_CS_TEST> { new MvcApplication31.TB_CS_TEST { REQ_NO = 0, SEQ_NO = 0, ITEM_ID = 0, QUANTITY = 0, UOM = "", UNIT_PRICE = 0, EXTENDED_AMT = 0 } };
            }
        }
        return View(ci);
    }
}

感谢您的帮助!

为了获得经验,我将执行后续步骤。

 if (ModelState.IsValid)
        {
            using (Entities dc = new Entities())
            {
                //decimal dcr = dc.TB_CS_TEST2.DefaultIfEmpty().Max(u => u.REQ_NO); THIS CODE PASS DE ID FROM THE OTHER TABLE AND PUT IN A VARIABLE AND USE IN SAVECHANGES

                foreach (var i in ci)
                {
                    decimal dcr = dc.TB_CS_TEST2.DefaultIfEmpty().Max(u => u.REQ_NO); 
                    i.REQ_NO = dcr;
                    i.CRT_DATE = DateTime.Now;
                    dc.TB_CS_TEST.Add(i);
                }
                //var cust = new TB_CS_TEST2();
                //var pedido = new TB_CS_TEST { TB_CS_TEST2 = cust };
                dc.SaveChanges();
                SendEmail();
                if (dc.SaveChanges() > 0)
                {
                    ViewBag.Message = "Data Successfully saved!";
                }
                else
                {
                    ViewBag.Message = "Error";
                }
                ModelState.Clear();
                //ci = new List<MvcApplication31.TB_CS_TEST> { new MvcApplication31.TB_CS_TEST { CONTACTID = 0, CONTACTNO = "", CONTACTPERSON = "" } };
                ci = new List<MvcApplication31.TB_CS_TEST> { new MvcApplication31.TB_CS_TEST { REQ_NO = 0, SEQ_NO = 0, ITEM_ID = 0, QUANTITY = 0, UOM = "", UNIT_PRICE = 0, EXTENDED_AMT = 0 } };
            }
        }
        return View(ci);