我正在寫一個帶有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)
請問,你可以給我這樣做的最佳方式的方向?
請發佈視圖的所有代碼。 – Sxntk
這是因爲您沒有正確生成窗體控件(但您沒有顯示您的代碼!) –