如何将新创建的ID从Order Controller传递到MVC中的Order Item Controller?

时间:2015-08-21 18:17:38

标签: c# asp.net-mvc asp.net-mvc-4 controller

我想要做的是将新创建的OrderID从我的Order Controller创建表单传递到OrderItem Controller创建表单,并添加与该特定OrderID I相关的Order_Items刚过去了。我设法重定向“创建订单”按钮,直接转到OrderItem索引表单,但是当我想添加新的{时,它仍然会拉动所有先前创建的OrderIDs,包括新的OrderID {1}}。我希望表单只提取我刚刚创建的OrderItems,而不是之前创建的所有其他OrderID

此外,我将其重定向到订单商品索引表单的原因是,我可以看到与该特定OrderIDs相关的所有订单商品,但它仍显示所有OrderID为每个OrderItems创建。

我不知道如何做到这一点,因为我对MVC很新,所以非常感谢所有帮助。

这是订单控制器的代码:

OrderID

这是Order_Item控制器的代码:

   using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Entity;
    using System.Linq;
    using System.Net;
    using System.Web;
    using System.Web.Mvc;
    using HealthHabitat.DAL;
    using HealthHabitat.Models;

    namespace HealthHabitat.Controllers
    {
          {
            private HealthContext db = new HealthContext();

        // GET: Order
        public ActionResult Index()
        {
            var orders = db.Orders.Include(o => o.Delivery).Include(o => o.Hospital).Include(o => o.Staff);
            return View(orders.ToList());
        }

        // GET: Order/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Order order = db.Orders.Find(id);
            if (order == null)
            {
                return HttpNotFound();
            }
            return View(order);
        }

        // GET: Order/Create
        public ActionResult Create()
        {
            ViewBag.HospitalID = new SelectList(db.Hospitals, "HospitalID", "Name");
            ViewBag.StaffID = new SelectList(db.Staffs, "StaffID", "First_Name");
            return View();
        }

        // POST: Order/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "OrderID,HospitalID,StaffID,Date,Time")] Order order)
        {
            if (ModelState.IsValid)
            {
                db.Orders.Add(order);
                db.SaveChanges();

                return RedirectToAction("Index","OrderItem", new { id = "OrderID" });
            }


            ViewBag.HospitalID = new SelectList(db.Hospitals, "HospitalID", "Name", order.HospitalID);
            ViewBag.StaffID = new SelectList(db.Staffs, "StaffID", "First_Name", order.StaffID);
            return View(order);
        }

        // GET: Order/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Order order = db.Orders.Find(id);
            if (order == null)
            {
                return HttpNotFound();
            }
            ViewBag.DeliveryID = new SelectList(db.Deliverys, "DeliveryID", "DeliveryID", order.DeliveryID);
            ViewBag.HospitalID = new SelectList(db.Hospitals, "HospitalID", "Name", order.HospitalID);
            ViewBag.StaffID = new SelectList(db.Staffs, "StaffID", "First_Name", order.StaffID);
            return View(order);
        }

        // POST: Order/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "OrderID,HospitalID,StaffID,DeliveryID,Date,Time")] Order order)
        {
            if (ModelState.IsValid)
            {
                db.Entry(order).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.DeliveryID = new SelectList(db.Deliverys, "DeliveryID", "DeliveryID", order.DeliveryID);
            ViewBag.HospitalID = new SelectList(db.Hospitals, "HospitalID", "Name", order.HospitalID);
            ViewBag.StaffID = new SelectList(db.Staffs, "StaffID", "First_Name", order.StaffID);
            return View(order);
        }

        // GET: Order/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Order order = db.Orders.Find(id);
            if (order == null)
            {
                return HttpNotFound();
            }
            return View(order);
        }

        // POST: Order/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Order order = db.Orders.Find(id);
            db.Orders.Remove(order);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
    }

订单型号

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using HealthHabitat.DAL;
using HealthHabitat.Models;

namespace HealthHabitat.Controllers
{
    public class OrderItemController : Controller
    {
        private HealthContext db = new HealthContext();

        // GET: OrderItem
        public ActionResult Index()
        {

            var order_Items = db.Order_Items.Include(o => o.Item).Include(o => o.Order);

            return View(order_Items.ToList());


        }

        // GET: OrderItem/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Order_Item order_Item = db.Order_Items.Find(id);
            if (order_Item == null)
            {
                return HttpNotFound();
            }
            return View(order_Item);
        }

        // GET: OrderItem/Create
        public ActionResult Create()
        {

            ViewBag.ItemID = new SelectList(db.Items, "ItemID", "Name");
            ViewBag.OrderID = new SelectList(db.Orders, "OrderID", "OrderID");
            return View();
        }

        // POST: OrderItem/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "Order_ItemID,OrderID,ItemID,Type,Quantity")] Order_Item order_Item)
        {
            if (ModelState.IsValid)
            {

                db.Order_Items.Add(order_Item);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.ItemID = new SelectList(db.Items, "ItemID", "Name", order_Item.ItemID);
            ViewBag.OrderID = new SelectList(db.Orders, "OrderID", "OrderID", order_Item.OrderID);
            return View(order_Item);
        }

        // GET: OrderItem/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Order_Item order_Item = db.Order_Items.Find(id);
            if (order_Item == null)
            {
                return HttpNotFound();
            }
            ViewBag.ItemID = new SelectList(db.Items, "ItemID", "Name", order_Item.ItemID);
            ViewBag.OrderID = new SelectList(db.Orders, "OrderID", "OrderID", order_Item.OrderID);
            return View(order_Item);
        }

        // POST: OrderItem/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "Order_ItemID,OrderID,ItemID,Type,Quantity")] Order_Item order_Item)
        {
            if (ModelState.IsValid)
            {
                db.Entry(order_Item).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.ItemID = new SelectList(db.Items, "ItemID", "Name", order_Item.ItemID);
            ViewBag.OrderID = new SelectList(db.Orders, "OrderID", "OrderID", order_Item.OrderID);
            return View(order_Item);
        }

        // GET: OrderItem/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Order_Item order_Item = db.Order_Items.Find(id);
            if (order_Item == null)
            {
                return HttpNotFound();
            }
            return View(order_Item);
        }

        // POST: OrderItem/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Order_Item order_Item = db.Order_Items.Find(id);
            db.Order_Items.Remove(order_Item);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}

OrderItem模型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace HealthHabitat.Models
{
    public class Order
    {
        public int OrderID { get; set; }

        [Display(Name = "Hospital")]
        public int HospitalID { get; set; }

        [Display(Name = "Staff")]
        public int StaffID { get; set; }
        public int? DeliveryID { get; set; }

        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        public DateTime Date { get; set; }

        [DataType(DataType.Time)]
        [DisplayFormat(DataFormatString = "{0:H:mm}", ApplyFormatInEditMode = true)]
        public DateTime Time { get; set; }

        public virtual Hospital Hospital { get; set; }
        public virtual Staff Staff { get; set; }
        public virtual Delivery Delivery { get; set; }
        public virtual ICollection<OrderItem> OrderItems { get; set; }
    }
}

项目模型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace HealthHabitat.Models
{
    public class OrderItem
    {

        public int OrderItemID { get; set; }
        public int OrderID { get; set; }
        public int ItemID { get; set; }

        [Required]
        public string Type { get; set; }

        public int Quantity { get; set; }

        public virtual Order Order { get; set; }
        public virtual Item Item { get; set; }

    }
}

2 个答案:

答案 0 :(得分:1)

有点不清楚OrderItemControllerDetails()方法的目的是什么。您只需显示所有订单的所有订单商品。通常,OrderController Details()方法会显示订单的详细信息以及与该订单相关联的项目列表(可能是一个表格,显示每个OrderItem的所有详细信息,包括要编辑/删除的链接,或者如果在一页上显示太多信息,则链接到OrderItemController public ActionResult Index() { // Display a collection on all orders } public ActionResult Details(int ID) { // Display the details of an order, including a collection of its order items } public ActionResult Create() { // Create a new Order } [HttpPost] public ActionResult Create(Order model) { // Save the order return RedirectToAction("Details", new { ID = model.ID }); } 方法

我会建议以下结构

OrderController

Details.cshtml

OrderItem视图将包含一个链接(菜单项),以创建与Order相关联的新@Html.ActionLink("Create Item", "Create", "OrderItem", new { ID = Model.OrderID }, null)

public ActionResult Create(int ID) // ID is the ID of the Order
{
  OrderItem model = new OrderItem() { OrderID = ID };
  return View(model);
}
[HttpPost]
public ActionResult Create(OrderItem model)
{
  // Save the OrderItem and redirect to the order details page
  return RedirectToAction("Details", "Order", new { ID = model.OrderID });
}

OrderItemController

Create.cshtml

OrderDetails的{​​{1}}视图中,在表单中包含OrderID作为路由值,或在表单中包含隐藏的输入

@model OrderDetail
@using (Html.BeginForm(new { OrderID = Model.OrderID }))
{
  ....
}

附注:建议您遵循常规命名约定 - OrderItem,而不是Order_Item - 并使用视图模型,尤其是创建/编辑和删除那些糟糕的[Bind(Include = "...")]属性。

答案 1 :(得分:0)

id的{​​{1}}操作中添加Index参数,并使用该ID过滤订单商品。

OrderItemController