英文:
"Create" Action Method Not Working ASP.NET Core
问题
下面是您要翻译的内容:
"When I add the following lines to the Tournament class the Create
action method doesn't work in the webapp It just reloads the page. I recorded a short clip to show what it does: https://youtu.be/pvanpQD8LhM. I am using .NET 7
Code causing the problem:
public ICollection<Game> Games { get; set; }
Here's the classes:
using System.ComponentModel.DataAnnotations;
namespace WebApplication3.Models
{
public class Tournament
{
public int TournamentId { get; set; }
[Required]
public string TournamentName { get; set; }
[Required]
public string Location { get; set; }
//public DateTime StartDate { get; set; }
//public DateTime EndDate { get; set; }
public ICollection<Game> Games { get; set; }
}
}
namespace WebApplication3.Models
{
public class Game
{
public int GameId { get; set; }
//public DateTime GameDate { get; set; }
public string Player1 { get; set; }
public string Player2 { get; set; }
public string Score { get; set; }
public int TournamentId { get; set; }
public Tournament Tournament { get; set; }
}
}
I first tried different code from a Tutorial, which didn't work. I got this from the EFTutorial website. I have also tried removing the required annotations.
Edit:
Here's the create action method for Tournament:
public IActionResult Create()
{
return View();
}
// POST: Tournament/Create
// To protect from overposting attacks, enable the specific properties you want to bind to.
// For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("TournamentId,TournamentName,Location")] Tournament tournament)
{
if (ModelState.IsValid)
{
_context.Add(tournament);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(tournament);
}
Here's the create action method for Game:
public IActionResult Create()
{
ViewData["TournamentId"] = new SelectList(_context.Tournament, "TournamentId", "TournamentId");
return View();
}
// POST: Game/Create
// To protect from overposting attacks, enable the specific properties you want to bind to.
// For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("GameId,Player1,Player2,Score,TournamentId")] Game game)
{
if (ModelState.IsValid)
{
_context.Add(game);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
ViewData["TournamentId"] = a SelectList(_context.Tournament, "TournamentId", "TournamentId", game.TournamentId);
return View(game);
}
希望这有所帮助!
英文:
When I add the following lines to the Tournament class the Create
action method doesn't work in the webapp It just reloads the page. I recorded a short clip to show what it does: https://youtu.be/pvanpQD8LhM. I am using .NET 7
Code causing the problem:
public ICollection<Game> Games { get; set; }
Here's the classes:
using System.ComponentModel.DataAnnotations;
namespace WebApplication3.Models
{
public class Tournament
{
public int TournamentId { get; set; }
[Required]
public string TournamentName { get; set; }
[Required]
public string Location { get; set; }
//public DateTime StartDate { get; set; }
//public DateTime EndDate { get; set; }
public ICollection<Game> Games { get; set; }
}
}
namespace WebApplication3.Models
{
public class Game
{
public int GameId { get; set; }
//public DateTime GameDate { get; set; }
public string Player1 { get; set; }
public string Player2 { get; set; }
public string Score { get; set; }
public int TournamentId { get; set; }
public Tournament Tournament { get; set; }
}
}
I first tried different code from a Tutorial, which didn't work. I got this from the EFTutorial website. I have also tried removing the required annotations.
Edit:
Here's the create action method for Tournament:
public IActionResult Create()
{
return View();
}
// POST: Tournament/Create
// To protect from overposting attacks, enable the specific properties you want to bind to.
// For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("TournamentId,TournamentName,Location")] Tournament tournament)
{
if (ModelState.IsValid)
{
_context.Add(tournament);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(tournament);
}
Here's the create action method for Game:
public IActionResult Create()
{
ViewData["TournamentId"] = new SelectList(_context.Tournament, "TournamentId", "TournamentId");
return View();
}
// POST: Game/Create
// To protect from overposting attacks, enable the specific properties you want to bind to.
// For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("GameId,Player1,Player2,Score,TournamentId")] Game game)
{
if (ModelState.IsValid)
{
_context.Add(game);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
ViewData["TournamentId"] = new SelectList(_context.Tournament, "TournamentId", "TournamentId", game.TournamentId);
return View(game);
}
答案1
得分: 0
@TJ_Frags,您可以尝试在create
方法中简单地重定向到索引页面。如果这样可以的话,那么您需要从if (ModelState.IsValid)
开始调试您的代码。我猜测您的RedirectToAction(nameof(Index));
可能无法执行,因为public ICollection<Game> Games { get; set; }
可能会使模型状态无效。
public IActionResult Create()
{
return View();
}
// POST: Tournament/Create
// 为了防止过度提交攻击,请启用要绑定的特定属性。
// 有关更多详细信息,请参阅 http://go.microsoft.com/fwlink/?LinkId=317598。
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("TournamentId,TournamentName,Location")] Tournament tournament)
{
return RedirectToAction(nameof(Index));
}
英文:
@TJ_Frags you might want to try and just simply redirect to index in your create method. If that works then you need to debug your code from if (ModelState.IsValid)
onwards. My guess is your RedirectToAction(nameof(Index));
is not reachable as public ICollection<Game> Games { get; set; }
might be invalidating model state.
public IActionResult Create()
{
return View();
}
// POST: Tournament/Create
// To protect from overposting attacks, enable the specific properties you want to bind to.
// For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("TournamentId,TournamentName,Location")] Tournament tournament)
{
return RedirectToAction(nameof(Index));
}
答案2
得分: 0
根据您在这篇帖子中的描述,我认为每次在HttpPost
创建操作中创建Tournament
模型时,ModelState.IsValid
都为false,然后您会运行return View(tournament)
,页面将重新加载并在视图中显示The Games field is required.
错误消息,对吗?
原因是从.NET 6开始,新项目在项目文件中包含了<Nullable>enable</Nullable>元素。这意味着即使您没有手动添加,项目也会默认添加[requried]属性。因此,当您创建Tournament
模型时,Games
字段为null,所以ModelState.IsValid
将为false,并返回The Games field is required.
错误消息。您可以只需在字段后面添加?
来使其可为空。
public ICollection<Game>? Games { get; set; }
英文:
From your description in this post, I think every time you create Tournament
model in HttpPost
create action, ModelState.IsValid
is false and you will run return View(tournament)
then the page will reload and show The Games field is required.
error message in your view, right?
The reason is Beginning with .NET 6, new projects include the <Nullable>enable</Nullable> element in the project file. It means the project will add [requried] attribute by default even if you don't add it manually. So when you create Tournament
model. The Games
field is null, So ModelState.IsValid
will be false and return The Games field is required.
error message. You can just add ?
to make this field nullable.
public ICollection<Game>? Games { get; set; }
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论