假设我有5个模型(例子让事情变得简单)
我想制作一份生产订单表格 - PONo。自动运行 - 我可以通过级联下拉列表选择销售订单和LineItemNo - 最后,ProductName,Quantity,UnitPrice和Group名称应自动填写我的生产订单表格并提交。
我并没有从所有教程中得到如何将所有表连接在一起。 在SQL中编写它将是
SELECT LineItemTBl.LineOrderNo, ProductTBL.ProductName
LineItemTBl.Quantity, ProductTBL.UnitPrice, GroupTBL.GroupName
FROM (LineItemTBL L INNER JOIN ProductTBL P ON L.ProductCode=P.ProductCode) INNER JOIN GROUPTBL G ON P.GroupID=G.GroupID
WHERE LineItemTBL.LineItemNo=... AND LineItemTBL.SONo=....;
ProductionOrderController.cs
// GET: ProductionTBLs
public ActionResult Index()
{
var productionTBLs = db.ProductionTBLs.Include(p => p.SalesOrderTBL).Include(p => p.SOLineItemTBL;
return View(productionTBLs.ToList());
}
// GET: ProductionTBLs/Create
public ActionResult Create()
{
ViewBag.SONo = new SelectList(db.SOTBLs, "SONo", "SONo");
return View();
}
// POST: ProductionTBLs/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "PONo, SoNo, LineItemNo, ProductName,Quantity, UnitPrice")] productionTBL ProductionTBL)
{
if (ModelState.IsValid)
{
db.ProductionTBLs.Add(productionTBL);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.SONo = new SelectList(db.SOTBLs, "SONo", "SONo", "productionTBL.SONo");
ViewBag.LineItemNo = new SelectList(db.LineItemTBLs, "LineItemNo", "LineItemNo", "productionTBL.LineItemNo");
}
ProductionOrderCreate.cshtml
@Html.DropDownList("SONo", null, htmlAttributes: new { @class = "form-control" })
@Html.DropDownList("LineItemNo", null, htmlAttributes: new { @class = "form-control" })
@Html.DisplayFor(modelItem => item.ProductTBL.ProductName)
@Html.DisplayFor(modelItem => item.GroupTBL.GroupName)