2017-03-08 85 views
-1

我正在写一个带有MVC和实体框架的网页。 我有附加订单项的订单,并希望将复杂对象返回给控制器进行处理。MVC传递一个复杂对象到控制器保存

我现在已经包含了所有的代码。

我的观点

@model BCMManci.ViewModels.OrderCreateGroup 

@{ 
    ViewBag.Title = "Create"; 
} 
<h2>New Order</h2> 

@using (Html.BeginForm()) 
{ 
    @Html.AntiForgeryToken() 

    <h4>@Html.DisplayFor(model => model.Order.Customer.FullName)</h4> 

    <table> 
     <tr> 
      <td><b>Order Date:</b> @Html.DisplayFor(model => model.Order.OrderDate)</td> 
      <td><b>Status:</b> @Html.DisplayFor(model => model.Order.OrderStatus.OrderStatusName)</td> 
     </tr> 
     <tr> 
      <td colspan="2"> 
       <b>Notes</b> 
       @Html.EditorFor(model => model.Order.Notes, new { htmlAttributes = new { @class = "form-control" } }) 
      </td> 
     </tr> 
    </table> 
     @Html.ValidationMessageFor(model => model.Order.Notes, "", new { @class = "text-danger" }) 
     <hr /> 
     @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 


     <table class="table table-striped table-hover"> 
      <thead> 
       <tr> 
        <td>Name</td> 
        <td>Price</td> 
        <td>Discount</td> 
        <td>Total</td> 
        <td>Quantity</td> 
       </tr> 
      </thead> 
      <tbody> 
       @foreach (var product in Model.ProductWithPrices) 
       { 
        <tr> 
         <td> 
          @Html.DisplayFor(modelItem => product.ProductName) 
         </td> 
         <td> 
          @Html.DisplayFor(modelItem => product.SellingPrice) 
         </td> 
         <td> 
          @Html.DisplayFor(modelItem => product.DiscountPrice) 
         </td> 
         <td> 
          @Html.DisplayFor(modelItem => product.TotalPrice) 
         </td> 
         <td> 
          @Html.EditorFor(modelItem => product.Quantity, new { htmlAttributes = new { @class = "form-control" } }) 
         </td> 
        </tr> 
       } 
      </tbody> 
     </table> 

     <input type="submit" value="Create" class="btn btn-default" /> 
} 
<div class="btn btn-danger"> 
    @Html.ActionLink("Cancel", "Index") 
</div> 

@section Scripts { 
    @Scripts.Render("~/bundles/jqueryval") 

}

控制器

[HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult Create([Bind(Include = "Order,ProductWithPrices,Order.Note,product.Quantity")] OrderCreateGroup order) 
    { 
     try 
     { 
      if (ModelState.IsValid) 
      { 
       db.Orders.Add(order.Order); 

       foreach (var orderItem in order.ProductWithPrices.Select(item => new OrderItem 
       { 
        OrderId = order.Order.OrderId, 
        ProductId = item.ProductId, 
        Quantity = item.Quantity, 
        ItemPrice = item.SellingPrice, 
        ItemDiscount = item.DiscountPrice, 
        ItemTotal = item.TotalPrice 
       })) 
       { 
        db.OrderItems.Add(orderItem); 
       } 
       db.SaveChanges(); 
       return RedirectToAction("ConfirmOrder", new {id = order.Order.OrderId}); 
      } 
     } 
     catch (DataException /* dex */) 
     { 
      //TODO: Log the error (uncomment dex variable name and add a line here to write a log. 
      ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator."); 
     } 
     ViewBag.Products = db.Products.Where(model => model.IsActive == true); 

     PopulateDropdownLists(); 
     return View(order); 
    } 

数据源

public class OrderCreateGroup 
{ 
    public OrderCreateGroup() 
    { 
     ProductWithPrices = new List<ProductWithPrice>(); 
    } 

    public Order Order { get; set; } 
    public ICollection<ProductWithPrice> ProductWithPrices { get; set; } 
} 

public class ProductWithPrice : Product 
{ 
    public decimal SellingPrice { get; set; } 
    public decimal DiscountPrice { get; set; } 

    public int Quantity { get; set; } 

    public decimal TotalPrice { get; set; } 
} 

但是,表单中输入的值未被传递。所以我无法在控制器中访问它们。尽管网页中存在数据,但'productWithPrices'集合为空。

我试图使它ASYC也试图改变ActionLink的按钮,如下图所示,但它并没有得到控制。

@Html.ActionLink("Create", "Create", "Orders", new { orderCreateGoup = Model }, null) 

这是控制器,但它现在没有意义,因为参数在页面的数据源中传递。

public ActionResult Create(OrderCreateGroup orderCreateGoup) 

请问,你可以给我这样做的最佳方式的方向?

+0

请发布视图的所有代码。 – Sxntk

+0

这是因为您没有正确生成窗体控件(但您没有显示您的代码!) –

回答

0

在你OrderCreateGroup类初始化集合到一个空列表。

public class OrderCreateGroup 
{ 
    public OrderCreateGroup() 
    { 
     ProductWithPrices = new List<ProductWithPrice>(); 
    } 
    public Order Order { get; set; } 
    public ICollection<ProductWithPrice> ProductWithPrices { get; set; } 
} 

你需要添加@Html.HiddenFor(m => m.SellingPrice),类似的还有,如果你要发布他们回到控制器使用DisplayFor其他绑定字段。

注:为了你的利益,试图看看生成的HTML代码,当您的网页浏览器中呈现,看到了<form>标签用name属性中生成的标记。

+0

谢谢。该排序的产品价格不会返回null。但是,它们仍然是空的。 –

+0

嘿@ColinGenricks问题在于您的价格必然会使用DisplayFor进行查看。 DisplayFor不会将值发回控制器。您需要为每个需要回发给控制器的价格属性提供@ Html.HiddenFor()。 – Jinish

+0

谢谢。这是问题所在。我添加了隐藏字段,现在这些值正在通过订单 –

0

确保你从复杂的对象绑定相应的属性,如下所示:

@model BCMManci.ViewModels.OrderCreateGroup 

... 
@using (Html.BeginForm()) 
{ 
    @Html.AntiForgeryToken() 

    <div class="form-horizontal"> 


     ... 
     <div class="form-group"> 
      @Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.OrderCreateGroup.Order.Quantity, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.OrderCreateGroup.Order.Quantity, "", new { @class = "text-danger" }) 
      </div> 
     </div> 



     <div class="form-group"> 
      <div class="col-md-offset-2 col-md-10"> 
       <input type="submit" value="Create" class="btn btn-default" /> 
      </div> 
     </div> 
    </div> 
} 

<div> 
    @Html.ActionLink("Back to List", "Index") 
</div> 

注意:model.OrderCreateGroup.Order.Quantity将是一个订单的财产。 希望这有助于。

+0

我试图实施您的建议。但是,由于数据源是OrderCreateGroup,我使用的是:'model.Order.Notes'而不是'model.OrderCreateGroup.Order.Notes'。它不允许我这样做。这是否意味着我的数据源设置不正确? –

相关问题