Добрый день
Я новичок в MVC и отношениях с базой данных. Итак, я создаю систему услуг (заказ товаров). Я понимаю, что должно быть отношение 1: N (один ко многим), так как клиент может заказать много товаров. У меня есть 2 таблицы, первая таблица хранит данные человека, запрашивающего услугу, и сгенерированный в этом случае идентификатор переменной REQ_NO, а вторая таблица хранит продукты, запрошенные клиентом. Моя проблема заключается в следующем: как я могу назначить идентификатор клиента, сгенерированный в базе данных, для продуктов, которые он запрашивает, например: если клиент создан, идентификатор 304 запрашивает 2 продукта, этим продуктам назначается идентификатор 304
ПРИМЕЧАНИЕ:
- TB_CS_TEST2 - для данных клиента и req_no
- TB_CS_TEST - для продуктов с назначенной req_no формой TB_CS_TEST2
Здесь мой код
Класс
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);
}
}
Спасибо за помощь!