【发布时间】:2015-08-04 00:18:32
【问题描述】:
这是我的表格:统计数据
Id,Depth,RefreshCounter
样本记录:
Id Depth RefreshCounter
1 1 1
2 1 0
3 1 0
4 1 0
现在我需要做的是,每当我刷新页面时,我需要在深度为 1 的数据库表中将此 refreshcounter 值增加 1。
我在加载我的视图页面时这样调用这个方法:
@Html.Action("IncrementRefreshcounter", "Statistics", new { id = 1}) //for eg:1,2,3,4
这是我的代码:
[ChildActionOnly]
public ActionResult IncrementRefreshcounter(int id)
{
using ( var context = new MyDBContext())
{
//at each page refresh i would be passing different id to my controller from view.for eg 1,2,3,4
var data=context.Statistics.where(x=>x.Depth==1 && r.Id==id).FirstorDefualt();
context.RefreshCounter++;//Increment here RefreshCounter.
context.SaveChangesAsync();
return PartialView("xxx")
}
}
当我的 View Loads.Problem 是当我第一次运行我的应用程序并调用此方法时,我调用此方法,它成功地将 RefreshCounter 更新了 1,但之后每当我刷新我的页面并调用此方法时,它从未更新 RefreshCounter Depth=1 的记录。
在我的示例记录中,您可以看到深度为 1 的 Id 1 具有值为 1 的刷新计数器,因为这是我第一次运行我的应用程序并且它已成功更新该值,但之后它不再更新任何值例如:Id 2 Depth 1
RefreshCounter 只增加 1 次,但之后它不再增加该变量。
谁能告诉我问题是什么SaveChangesAsync??
【问题讨论】:
-
你想用
Depth==1更新其他记录还是只更新第一条记录? -
@MehrdadKamelzadeh:其他记录也不仅仅是 depth==1 和 id==1.others 记录,因为在每次刷新时我都会将不同的 id 传递给我的控制器
-
那你为什么写
where(x=>x.Depth==1).FirstorDefualt()??它只返回条件找到的第一条记录。我的意思是它会找到他们Depth==1的所有记录,然后它只返回第一个!
标签: c# asp.net-mvc entity-framework async-await