简体   繁体   English

在ASP.NET MVC中编辑订单详细信息

[英]Edit order details in ASP.NET MVC

The standard way to edit a record in ASP.NET MVC is the following: 在ASP.NET MVC中编辑记录的标准方法如下:

//
// GET: /Movies/Edit/5
public ActionResult Edit(int id = 0)
{
    Movie movie = db.Movies.Find(id);
    if (movie == null)
    {
        return HttpNotFound();
    }
    return View(movie);
}

//
// POST: /Movies/Edit/5
[HttpPost]
public ActionResult Edit(Movie movie)
{
    if (ModelState.IsValid)
    {
        db.Entry(movie).State = EntityState.Modified;
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    return View(movie);
}

The problem is that I need to edit order details (1 order, many details) and therefore based on two IDs (the order and the product ones). 问题是我需要编辑订单详细信息(1个订单,许多详细信息),因此需要基于两个ID(订单和产品ID)进行编辑。 It does not work (I cannot get an OrderDetail item as action parameter). 它不起作用(我无法获得OrderDetail项作为操作参数)。 How can I solve this problem? 我怎么解决这个问题?

Thanks. 谢谢。

//
// GET: /Orders/EditDetails
public ActionResult EditDetails(int id, string productID)
{
   OrderDetail od = GetOrderDetail(id, productID);

   return View(od);
}

//
// POST: /Orders/EditDetails
[HttpPost]
public ActionResult EditDetails(OrderDetail od)
{
   if (ModelState.IsValid)
   {
      context.Entry(od).State = EntityState.Modified;
      context.SaveChanges();
      return RedirectToAction("Index");
   }
   return View(od);
}

EDIT: Here is the code requested by Shimmy: 编辑:这是Shimmy请求的代码:

@using (Html.BeginForm("EditDetails", "Orders", FormMethod.Post))
{
   @Html.LabelFor(m => m.quantity)
   @Html.TextBoxFor(m => m.quantity)

   @Html.LabelFor(m => m.productID)
   @Html.DropDownListFor(m => m.productID, new SelectList((IEnumerable)ViewBag.productList, "productID", "fullProductName"))

   @Html.HiddenFor(model => model.orderID)
}

Make sure the OrderDetail.Id itself as well as the OrderDetail.OrderId and the OrderDetail.MovieId properties are all present in the form as a hidden field. 确保OrderDetail.Id本身以及OrderDetail.OrderIdOrderDetail.MovieId属性都以隐藏字段的形式显示在表单中。

In that way, when you post it back to the server you have track on what Movie and Order this OrderDetail is of, in the action. 这样,当您将其发布回服务器时,就可以在操作中跟踪此OrderDetail MovieOrder

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM