我想創建一個按鈕,如果我單擊它,它會更改資料庫中表的整個列。請考慮以下代碼:
模型:
using Player_Simulation.Data;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace Player_Simulation.Models
{
public class Player
{
public int PlayerId { get; set; }
public string PlayerName { get; set; }
public int PlayerRating { get; set; }
public int PlayerYear { get; set; }
public int PlayerSkill { get; set; }
public string PlayerCountry { get; set; }
public ICollection<Match> Matches { get; set; }
public Player()
{
PlayerYear = 1;
PlayerRating = 1000;
}
}
}
控制器:
namespace Player_Simulation.Controllers
{
public class PlayerController : Controller
{
private readonly DataContext _database;
public PlayerController(DataContext database)
{
_database = database;
}
[HttpPost]
public void IncrementYear()
{
using (_database)
{
foreach (Player player in _database.Players)
{
if (player != null)
{
int oldYear = player.PlayerYear;
player.PlayerYear = oldYear 1;
_database.SaveChanges();
}
}
}
}
}
}
看法:
// removed code for shorter post
// the button to click so I can increment the 'year' column by 1
<a asp-controller="Player" asp-action="IncrementYear" class="btn btn-info">Increment Year</a>
圖形:

最終,我想要一個可以將所有年份遞增 1 的按鈕。它不需要回傳 View 或類似的東西。但是,在參考:
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/440797.html
