ASP.Net MVC视图模型>调用UpdateModel时的模型问题

时间:2011-03-14 17:05:55

标签: asp.net-mvc-3

我有一个问题,我可以使用一些帮助。我有一个MVC 3应用程序,我试图在回发编辑视图时获取表单值,但我显然是错误的。

我有3个表,其中实体框架对象映射到它们。请求者可以拥有一个订单,该订单又可以有多个OrderDetails

我还为每个实体创建了View Models,我还创建了一个名为Edit的强类型视图,它显示了所有信息,并使用以下代码填充ViewModels - 这一切都可以正常用于背景信息:

 //Grab the Requestor and corresponding order from the database
        var requestors = from r in db.Requestors
                         where r.RequestorID == id
                         join o in db.Orders on r.RequestorID equals o.RequestorID
                         select
                         new RequestorViewModel
                         {
                             RequestorID = r.RequestorID,
                             FirstName = r.FirstName,
                             LastName = r.LastName,
                             AddressStreet = r.AddressStreet,
                             AddressNumber = r.AddressNumber,
                             AddressCity = r.AddressCity,
                             AddressPostalCode = r.AddressPostalCode,
                             AddressProvince = r.AddressProvince,
                             Individual = r.Individual,
                             Organization = r.Organization,
                             PhoneNumber = r.PhoneNumber,
                             Email = r.Email,
                             Comment = r.Comment,
                             Order = new OrderViewModel
                             {
                                 OrderID = o.OrderID,
                                 TotalPamphlets = o.TotalPamphlets,
                                 OrderCompletionDate = o.OrderCompletionDate,
                                 OrderDate = o.OrderDate,
                                 DateRequired = o.DateRequired
                             }
                         };

        RequestorViewModel requestor = requestors.Single<RequestorViewModel>();

        //Get the OrderDetails associated with the Order
        var orderdetails = from od in db.OrderDetails
                           where od.OrderID == requestor.Order.OrderID
                           select new OrderDetailViewModel
                           {
                               OriginalQuantity = od.OriginalQuantity,
                               UpdatedQuantity = od.UpdatedQuantity,
                               PamphletID = od.PamphletID
                           };

        requestor.Order.OrderDetails = orderdetails.ToList<OrderDetailViewModel>();

        return requestor;

我的问题是当我尝试更新数据库时,唯一更新的是请求者信息。

我的编辑操作如下所示:

    [HttpPost]
    public ActionResult Edit(int ID, FormCollection formValues)
    {

        var query = from r in db.Requestors where r.RequestorID == ID select r;
        var req = query.SingleOrDefault();

        UpdateModel(req);
        db.SaveChanges();

        return View("Index");
    }

我的编辑视图示例如下: ![编辑视图] [2]

对于Requestor和Order部分,我使用Visual Studio生成的默认模板,编辑器如下所示:

请求者:@Html.EditorFor(model => model.FirstName)

订单:@Html.EditorFor(model => model.Order.OrderCompleted)

在我的OrderDetails部分中,我使用以下约定命名我的控件(它们处于循环中(为简洁起见缩短)

 @foreach (var od in Model.Order.OrderDetails) {
    int i = 0;
@Html.TextBox("OrderDetails[" + i + "].OriginalQuantity", od.OriginalQuantity)
i++
}

当我查看Form Collection时,Requestor,Order和Orderdetail的键如下所示(一些例子,因为我无法发布屏幕截图):

[1] RequestorID [17] Order.OrderCompleteed [22]订单明细[0] .OriginalQuantity

是否由于我没有明确地将OrderViewModel和OrderDetailViewModel转换为各自的EF实体?或者是我做错了什么?非常感谢任何帮助。

杰森

1 个答案:

答案 0 :(得分:0)

您说您有3个表,其中实体框架对象映射到它们。我建议在三个实体之间创建关联。如果您希望EF附加关联实体,则需要在实体上创建关联,因此您可能在两个表(来回)上都有关联。

如果您正在进行服务器绑定,那么在两个表上都有关联是正常的,但如果您正在进行Ajax绑定,则有时会出现与循环引用相关的问题。创建关联后,您可以包含实体,EF将根据需要管理连接并更新数据。

您可以使用XML编辑器打开您的EF,但您不需要;我只是从XML编辑器中抓取它,以便轻松地将其粘贴到此处,并为您提供所需内容的视觉效果。这只创建了一个从请求者到订单的关联,一个从order到order_item,使用你的外键值,visual studio painter使它变得简单。

 // one to many association between order and order_line
 <Association Name="FK_xxx">
      <End Role="order" Type="LINKDIRModel.Store.order" Multiplicity="1" />
      <End Role="order_line" Type="LINKDIRModel.Store.order_line" Multiplicity="*" />
      <ReferentialConstraint>
        <Principal Role="order">
          <PropertyRef Name="order_line" />
        </Principal>
        <Dependent Role="order_line">
          <PropertyRef Name="order_id" />
        </Dependent>
      </ReferentialConstraint>
  </Association>

 // controller code... for delete int order lines and updating order
 [HttpPost, ActionName("OrderDelete")]
 [GridAction]
 public ActionResult DeleteRequestor(int id)
 {
     // get order from EF along with all order_items.
     order myOrder = db.order.Include("order_line").Single(l => l.requestor_id == id);

     // We can delete a few order_lines and update the order easily
     List<order_line> myLines = 
        db.order.order_line.Where(o => o.order_id == id && o.order_amt == 1.00).ToList();

     // update the order
     MyOrder.order_comment = "deleted all the items with price of one dollar!";
     MyOrder.order_total = MyLines.Take(myLines.Count()).Sum(e => e.order_line_amt);
     MyOrder.DeleteObject(myLines);

     // save changes.
     db.SaveChanges();

    return View(new GridModel(db.requestor));
  }