谁可以帮助我?我有一个指向另一个页面的链接。和 我想统计这个链接被点击的次数并存储它 在我的 SQL Server 数据库中。我的页面是 ASP.NET Core razor 页面。我 我的数据库表中有一个列
variabelSearch
,每次单击链接时我都必须将此列增加 +1。
Public void OnGetLeagueAsync(Guid? varLgPlayerId)
{
HttpContext.Session.SetString("SessionLgPlayerId", varLgPlayerId.ToString());
Response.Redirect("/LeaguePlayerDetails");
variabelSearch++;
}
<td>
<a asp-page-handler="League" asp-route-varLgPlayerId="@itemP.id">@itemP.naam</a>
</td>
首先,您需要一个包含属性的表
variabelSearch
:
public class SearchCount
{
// Other properties...
public int VariabelSearch { get; set; }
}
然后在你的后端代码中:
public async Task OnGetLeagueAsync(Guid? varLgPlayerId)
{
HttpContext.Session.SetString("SessionLgPlayerId", varLgPlayerId.ToString());
// Assuming _context is your database context,
//and you have a known ID for the row you're updating. e.g. your passing varLgPlayerId
var searchCount = await _context.SearchCounts.FindAsync(knownId);
if (searchCount != null)
{
searchCount.VariabelSearch += 1;
await _context.SaveChangesAsync();
}
Response.Redirect("/LeaguePlayerDetails");
}