如何使用一种操作方法 (ASP.NET) 更新数据库中 table 的所有行?
How to update all rows of a table in database using one Action method (ASP.NET)?
我想创建一个按钮,如果我点击它,它会更改数据库中 table 的整个列。请考虑以下代码:
型号:
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 的按钮。它不需要 return 视图或类似的东西。但是,在参考: and codegrepper.com后,我得到一个错误 405。请参阅上面在 PlayerController 上的 Action 方法调用 IncrementYear 以供我尝试。
如何正确更新所有这些数据行,使所有年份都递增?如果有任何帮助,我将不胜感激!
您使用 [httppost]
在您的 IncrementYear ation 中接收一个 post 请求,因此您需要在您的视图中发送一个 post 请求。
将按钮更改为:
<form method="post">
<button asp-controller="Player" asp-action="IncrementYear" class="btn btn-info">Increment Year</button>
</form>
控制器
public class PlayerController : Controller
{
private readonly TableContext _context;
public PlayerController(TableContext tablecontext)
{
_context = tablecontext;
}
[HttpPost]
public IActionResult IncrementYear()
{
foreach (var play in _context.players.ToList())
{
play.PlayerYear++;
_context.SaveChanges();
}
return RedirectToAction("Privacy","Home");
}
}
我想创建一个按钮,如果我点击它,它会更改数据库中 table 的整个列。请考虑以下代码:
型号:
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 的按钮。它不需要 return 视图或类似的东西。但是,在参考:
如何正确更新所有这些数据行,使所有年份都递增?如果有任何帮助,我将不胜感激!
您使用 [httppost]
在您的 IncrementYear ation 中接收一个 post 请求,因此您需要在您的视图中发送一个 post 请求。
将按钮更改为:
<form method="post">
<button asp-controller="Player" asp-action="IncrementYear" class="btn btn-info">Increment Year</button>
</form>
控制器
public class PlayerController : Controller
{
private readonly TableContext _context;
public PlayerController(TableContext tablecontext)
{
_context = tablecontext;
}
[HttpPost]
public IActionResult IncrementYear()
{
foreach (var play in _context.players.ToList())
{
play.PlayerYear++;
_context.SaveChanges();
}
return RedirectToAction("Privacy","Home");
}
}