首先在代码中高效地更新多个记录
本文关键字:更新 记录 高效 代码 | 更新日期: 2023-09-27 18:05:41
在一个操作中有以下场景,我想更新给定购物车中所有商品的数量。我张贴回数组到我的购物车项目的控制器,并希望更新数量,因为用户已经更新了它们。我怎样才能做到这一点呢?这就是我现在的处境。
这是购物车的视图
@model SeniorProjectMVC.Models.ViewModels.CartViewModel
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h3>Your cart</h3>
@*<p>
@Html.ActionLink("Create New", "Create")
</p>*@
<div class="row header-row">
<div class="large-1 columns">
<h4> Quantity</h4>
</div>
<div class="large-5 columns text-center">
<h4>Name</h4>
</div>
<div class="large-1 columns">
<h4>Price</h4>
</div>
<div class="large-2 columns">
<h4>In stock</h4>
</div>
<div class="large-3 columns"></div>
</div>
@{
int CurrentIndex = 1;
}
@for (int i = 0; i < Model.CartArray.Length; i++)
{
<div class="@(CurrentIndex % 2 == 0 ? "row primary-row" : "row alternate-row")">
<div class="large-1 columns">
@Html.EditorFor( m => m.CartArray[i].Quantity)
</div>
<div class="large-5 columns text-center">
@Html.DisplayFor(m => m.CartArray[i].Sku.Product.Name)
</div>
<div class="large-1 columns">
@*@Html.DisplayFor(m => m.CartArray[i].Sku.Product.Price)*@
@String.Format("{0:c}", (Model.CartArray[i].Sku.Product.Price * Model.CartArray[i].Quantity))
</div>
<div class="large-2 columns">
@( Model.CartArray[i].Sku.Quantity > 0 ? "In Stock" : "Out of stock")
</div>
<div class="large-3 columns">
<ul class="button-group">
@*<li class="button">@Html.ActionLink("Edit", "Edit", new { id = Model.CartArray[i].ID }, new { @class = "button primary" }) </li>*@
<li class="button">@Html.ActionLink("Remove", "Remove", new { id = Model.CartArray[i].ID }, new { @class = "button primary" })</li>
</ul>
@*@Html.ActionLink("Details", "Details", new { id = item.ID }, new { @class = "button primary" }) |*@
</div>
</div>
{ CurrentIndex = CurrentIndex + 1; }
}
<div clsas="row">
<div class="large-offset-8 columns">
<h5 class="inline">Subtotal:</h5><h4 class="inline">@(Model.CartArray.Sum(p => (p.Sku.Product.Price * p.Quantity)).ToString("c"))</h4>
</div>
</div>
<div class="row">
<div class="medium-offset-8 medium-4 columns">
<ul class="button-group">
<li class="button">@Html.ActionLink("Update Quantities", "UpdateQuantities", "Cart", new { array = Model.CartArray }, new { @class = "button primary large" })</li>
@if (Request.IsAuthenticated && User.Identity.IsAuthenticated)
{
<li class="button">@Html.ActionLink("Checkout", "Address", "Checkout", null, new { @class = "button primary large" })</li>
}
else
{
<li class="button secondary">You must sign in to checkout</li>
}
</ul>
</div>
</div>
这是我想要将数量发送回控制器的操作,然后更新我的数据库上下文类。我不知道如何或什么是实现这一目标的最有效的方法。
public ActionResult UpdateQuantities(Models.ViewModels.CartViewModel cartModel)
{
for (int i = 0; i < cartModel.CartArray.Length; i++)
{
//either update each record, or build a list to do one mass update.
//This is where I am stuck
}
return View();
}
您应该能够像这样:
public ActionResult UpdateQuantities(Models.ViewModels.CartViewModel cartModel)
{
// instantiate your database context if it's not already done
using (var db = new Context())
{
for (int i = 0; i < cartModel.CartArray.Length; i++)
{
// update your records one at a time
db.YourItems.Where(i => <your_criteria>).Member = NewValue;
}
// commit your changes only once at the end
db.SaveChanges();
return View();
}
根据你的模型数据的结构,你也可以像这样替换上面的for循环:
var items = db.YourItems.ToList();
items.ForEach(i => i.Member = cartModel.CartArray[i.Id].NewValue);
或者如果你需要更新几个属性:
items.ForEach(i => {
i.Member1 = cartModel.CartArray[i.Id].Value1;
i.Member2 = cartModel.CartArray[i.Id].Value2;
};