首先,我的英语非常糟糕,如果你不明白我可以再次问我。 我正在尝试做一个库存项目,基本上我有一个“产品”表,我保留我的所有产品和“ProductExit”表来记录我销售的产品数量。我想要做的是使用ExitProduct表的“Quantity”值减少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视图中的用户输入找到降低“Stock”值的方法。
答案 0 :(得分:0)
不只是:
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);
}