缺货消息传递到View,如果数量<= 0

时间:2018-12-05 12:55:14

标签: c# asp.net-mvc

问题是,如果我的购物车中有2件商品没有所需的库存量,它将仅显示其中一个的消息。我也不知道如何在catch {}部分传递信息。有人可以向我指出正确的方向吗,我对asp.net mvc和一般编程还是比较陌生的。

db中有一个约束来检查数量> = 0

控制器

public ActionResult CreateOrder()
{
    List<ItemViewModel> cart = (List<ItemViewModel>)Session["cart"];
    Check check = new Check();

    decimal s = 0;
    foreach (var item in cart)
    {
        var device = db.Devices.Single(d => d.DeviceID == item.Device.DeviceID);
        if (device.Quantity <= 0)
        {
            TempData["Message"] = string.Format("Out of stock for {0}. In stock: {1}. ", device.DeviceName, device.Quantity);
            return View("Cart");
        }
        device.Quantity -= item.Quantity;
        check.DateTime = DateTime.Now;
        check.CustomerName = User.Identity.Name;
        s = s + item.Device.Price.Value * item.Quantity;
        check.Device += item.Device.DeviceName + " x " + item.Quantity + " - $" + (item.Device.Price *= item.Quantity) + ", " ;
    }
    check.Total = (int)s;
    check.Tax = check.Total * 20 / 100;
    try
    {
        db.Checks.Add(check);
        db.SaveChanges();
        foreach (var item in cart.ToList())
        {
            cart.Remove(item);
        }
    }
    catch (DbUpdateException) 
    {
        TempData["Message"] = TempData;
        return View("Cart");
    }



    if (User.IsInRole(RoleName.Admin))
    {
        return RedirectToAction("Details", "Checks", new { id = check.CheckID });
    }
    return RedirectToAction("PurchaseDetails", "Checks", new { id = check.CheckID });

}

查看

@{
    ViewBag.Title = "Cart";
}
@using WebShop.ViewModels;

<h2>Cart</h2>
<br />
@using (@Html.BeginForm("Update", "Devices", FormMethod.Post))
{
    <div class="col-lg-12 col-md-12 col-sm-12">
        <table class="table table-bordered table-hover" cellpadding="2" cellspacing="2" border="1">
            <thead>
                <tr>
                    <th width="5%"></th>
                    <th>Device</th>
                    <th width="7%">Price</th>
                    <th width="7%">Quantity</th>
                    <th width="7%">Total</th>
                    <th width="9%"></th>
                </tr>
            </thead>
            <tbody>
                @{
                    decimal s = 0;
                    decimal total = 0;
                    int index = 1;
                    decimal one = 0;
                }
                @foreach (ItemViewModel item in (List<ItemViewModel>)Session["cart"])
                {
                    s = s + item.Device.Price.Value * item.Quantity;
                    one = item.Device.Price.Value;
                    total = item.Device.Price.Value * item.Quantity;
                            <tr>
                                <td><a href="@item.Device.Image"><img src="@item.Device.Image" width="50" height="50" /></a></td>
                                <td>@item.Device.DeviceName</td>
                                <td>$@one.ToString("n2")</td>
                                <td><input class="form-control" type="text" name="quantity" value="@item.Quantity" style="width: 50px"></td>
                                <td>
                                    $@total.ToString("n2")
                                    @{index++;}
                                </td>
                                <td>
                                    <button type="button" class="btn btn-bitbucket" onclick="location.href='@Url.Action("Remove", "Devices", new { id = item.Device.DeviceID })'">
                                        <i class="fa fa-remove"></i>
                                    </button> |
                                    <button type="submit" class="btn btn-bitbucket">
                                        <i class="fa fa-refresh"></i>
                                    </button>
                                </td>
                            </tr>
                }
                        <tr>
                            <td align="right" colspan="4">Total:</td>
                            <td>$@s.ToString("n2")</td>
                        </tr>
            </tbody>
        </table>
    </div>
}
<br />
<button class="btn btn-bitbucket" type="button" onclick="location.href='@Url.Action("Shop", "Devices")'">Continue shoping</button> 
<button class="btn btn-bitbucket" type="button" onclick="location.href='@Url.Action("CreateOrder", "Devices")'">Create order</button> | @TempData["Message"]

1 个答案:

答案 0 :(得分:0)

感谢mjwills指出问题的位置:) 不知道这是否是最好的解决方案,但是它正在起作用,所以我对此感到满意。

if (device.Quantity <= 0)
            {
                foreach (var outOfStockItem in cart)
                {
                    string messages = string.Format("Out of stock for {0}. In stock: {1}. | ", outOfStockItem.Device.DeviceName, outOfStockItem.Device.Quantity);
                    TempData["Messages"] += messages;
                }
                return View("Cart");
            }

Cart Image