【问题标题】:How to update my inventory stock from an input value如何从输入值更新我的库存
【发布时间】:2018-04-11 16:26:32
【问题描述】:

首先,我的英语太差了,如果你看不懂我可以再问我一次。 我正在尝试做一个库存项目,基本上我有一个“产品”表,我保存所有产品和一个“ProductExit”表来记录我销售的产品数量。我想要做的是使用我的 ExitProduct 表的“数量”值减少我的 Product 表中的库存值。

产品表/类:

public class Product
    {
        public int Id { get; set; }
        public int BarCode { get; set; }
        public string Name { get; set; }
        public int Price { get; set; }
        public int Stock { get; set; }
    }

ProductExit 表/类

public class ProductExit
{
    public int Id { get; set; }

    public int ProductId { get; set; }
    [ForeignKey("ProductId")]
    public virtual Products Product { get; set; }
    public List<Products> ProductCollection { get; set; }

    public int Quantity { get; set; }
}

我使用实体框架来创建数据库连接,并使用脚手架为每个表创建视图。

我创建新产品出口的控制器方法:

[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "Id,ProductId,Quantity")] ProductExit productExit)
    {
        if (ModelState.IsValid)
        {
            db.ProductExit.Add(productExit);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        ViewBag.ProductosId = new SelectList(db.Product, "Id", "Name", productExit.ProductId);
        return View(productExit);
    }

我的看法:

<div class="form-horizontal">
    <h4>MovimientoEntrada</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.ProductId, "ProductId", 
htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("ProductId", null, htmlAttributes: new { 
@class = "form-control" })
            @Html.ValidationMessageFor(model => model.ProductId, "", new { 
@class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Quantity, htmlAttributes: new { @class 
= "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Quantity, new { htmlAttributes = 
new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.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>

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

问题是我找不到使用 ProductExit 视图中的用户输入来减少“库存”值的方法。

【问题讨论】:

  • 您已标记 sql-server 但使用 .. 发布解决方案是 .net 吗?您应该使用代码中的用户存储过程来处理 tsql 代码。这将允许您将参数传递给数据库,并让数据库处理代码以减少您的数量。这将使数据专家可以帮助您。目前大部分 sql 社区都很难提供帮助。

标签: sql-server asp.net-mvc razor entity-framework-6


【解决方案1】:

不只是:

HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "Id,ProductId,Quantity")] ProductExit productExit)
    {
        if (ModelState.IsValid)
        {
            db.ProductExit.Add(productExit);
            var product = db.Product.Find(productExit.ProductId):
            product.Stock -= productExit.Quantity;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        ViewBag.ProductosId = new SelectList(db.Product, "Id", "Name", productExit.ProductId);
        return View(productExit);
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 2021-11-29
    • 1970-01-01
    • 1970-01-01
    • 2017-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多