C# 如何在[HttpPost]编辑方法中获取特定字段的数据库值?
我需要在HttpPost编辑方法中获取特定字段“Quantity”的数据库值,以便对其执行一些操作,然后更新其值。我使用的是实体框架6C# 如何在[HttpPost]编辑方法中获取特定字段的数据库值?,c#,asp.net,asp.net-mvc,entity-framework,C#,Asp.net,Asp.net Mvc,Entity Framework,我需要在HttpPost编辑方法中获取特定字段“Quantity”的数据库值,以便对其执行一些操作,然后更新其值。我使用的是实体框架6 [HttpPost] [ValidateAntiForgeryToken] public async Task<ActionResult> EditGoogles([Bind(Include = "ProductID,Name,Code,Quantity,etc)] SalesDetail salesdetail) { if (ModelS
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> EditGoogles([Bind(Include = "ProductID,Name,Code,Quantity,etc)] SalesDetail salesdetail)
{
if (ModelState.IsValid)
{
var oldValue = db.Entry(salesdetail).GetDatabaseValues();
//here i want to get the Quantity from database
int QtyBefore = ???;
db.Entry(salesdetail).State = EntityState.Modified;
salesdetail.Quantity = //do some operaion with QtyBefore Here and update
await db.SaveChangesAsync();
return Json(new { success = true });
}
return PartialView("_EditSales", salesdetail);
}
[HttpPost]
[ValidateAntiForgeryToken]
公共异步任务EditGoogles([Bind(Include=“ProductID、名称、代码、数量等)]SalesDetail SalesDetail)
{
if(ModelState.IsValid)
{
var oldValue=db.Entry(salesdetail).GetDatabaseValues();
//这里我想从数据库中获取数量
int QtyBefore=???;
db.Entry(salesdetail).State=EntityState.Modified;
salesdetail.Quantity=//在此处和更新之前使用QTY执行一些操作
等待db.saveChangesSync();
返回Json(新的{success=true});
}
返回PartialView(“编辑销售”,salesdetail);
}
您可以这样做:
if (ModelState.IsValid)
{
Models.YourTableName oldValue = db.YourTableName.Where(q=>q.Quantity==salesdetail.Quantity).Select(p=>p).SingleOrDefault();
oldValue.Quantity= YourNewValue;
...
在没有所有必要信息的情况下,我认为您需要:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> EditGoogles([Bind(Include = "ProductID,Name,Code,Quantity,etc)] SalesDetail salesdetail)
{
if (ModelState.IsValid)
{
var oldValue = db.Entry(salesdetail).GetDatabaseValues();
//here i want to get the Quantity from database
int QtyBefore = db.Set<SalesDetail>().FirstOrDefault(x => x.Quantity == yourquantityid)?.Quantity ;
db.Entry(salesdetail).State = EntityState.Modified;
salesdetail.Quantity = //do some operaion with QtyBefore Here and update
await db.SaveChangesAsync();
return Json(new { success = true });
}
return PartialView("_EditSales", salesdetail);
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task one.获取此解决方案的空引用验证???@Imran我还没有测试它,但您在这方面取得了领先。找到错误所在,尝试修复它,如果您无法解决问题,您可以更新您的问题以向我们提供更多详细信息,因为现在对我们来说这是一个大概的问题。您的问题是什么