首先在代码中有效地更新多个记录

时间:2016-09-05 21:21:30

标签: c# asp.net-mvc entity-framework ef-code-first linq-to-entities

我在操作中有以下方案,我想更新给定购物车中所有商品的数量。我将数组回发给我的购物车项目的控制器,并希望在用户更新它们时更新数量。我怎样才能做到这一点?我就在这里。

这是我购物车的视图

@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();
        }

1 个答案:

答案 0 :(得分:0)

你应该能够使用这样的东西:

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;
};