admin管理员组文章数量:1026308
I am working on an ASP.NET Core MVC project using a MySQL database for my bachelor's thesis. The application involves managing users (students, teachers, and admins) and their roles. I have the following models:
Models: User (Pouzivatel):
public class Pouzivatel
{
public int PouzivatelId { get; set; }
public string Meno { get; set; }
public string Email { get; set; }
public int RolaId { get; set; }
public Rola Rola { get; set; }
}
Role (Rola):
public class Rola
{
public int RolaId { get; set; }
public string Nazov { get; set; }
public ICollection<Pouzivatel> Pouzivatelia { get; set; }
}
DbContext:
public class AplikaciaDbContext : DbContext
{
public DbSet<Pouzivatel> Pouzivatelia { get; set; }
public DbSet<Rola> Rola { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Rola>()
.HasMany(r => r.Pouzivatelia)
.WithOne(p => p.Rola)
.HasForeignKey(p => p.RolaId);
}
}
Issue: I scaffolded a PouzivatelsController using Visual Studio to generate CRUD operations. When I try to create a user through the Create method, the user is not saved to the database, and it also doesn't show up in the list view. There are no errors in Visual Studio or the logs.
PouzivatelsController (Create method):
// GET: Pouzivatel/Create
public IActionResult Create()
{
ViewData["RolaId"] = new SelectList(_context.Role, "RolaId", "Nazov");
return View();
}
// POST: Pouzivatel/Create
[HttpPost]
[ValidateAntiFeryToken]
public async Task<IActionResult> Create(Pouzivatel pouzivatel)
{
if (ModelState.IsValid)
{
_context.Add(pouzivatel);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
ViewData["RolaId"] = new SelectList(_context.Role, "RolaId", "Nazov", pouzivatel.RolaId);
return View(pouzivatel);
}
Create.cshtml (View):
@model Pouzivatel
<form asp-action="Create">
<div class="form-group">
<label asp-for="Meno" class="control-label"></label>
<input asp-for="Meno" class="form-control" />
<span asp-validation-for="Meno" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Email" class="control-label"></label>
<input asp-for="Email" class="form-control" />
<span asp-validation-for="Email" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="RolaId" class="control-label"></label>
<select asp-for="RolaId" class="form-control" asp-items="ViewBag.RolaId"></select>
<span asp-validation-for="RolaId" class="text-danger"></span>
</div>
<button type="submit" class="btn btn-primary">Create</button>
</form>
Why is the user not being saved to the database? What steps can I take to troubleshoot and fix this issue?
I am working on an ASP.NET Core MVC project using a MySQL database for my bachelor's thesis. The application involves managing users (students, teachers, and admins) and their roles. I have the following models:
Models: User (Pouzivatel):
public class Pouzivatel
{
public int PouzivatelId { get; set; }
public string Meno { get; set; }
public string Email { get; set; }
public int RolaId { get; set; }
public Rola Rola { get; set; }
}
Role (Rola):
public class Rola
{
public int RolaId { get; set; }
public string Nazov { get; set; }
public ICollection<Pouzivatel> Pouzivatelia { get; set; }
}
DbContext:
public class AplikaciaDbContext : DbContext
{
public DbSet<Pouzivatel> Pouzivatelia { get; set; }
public DbSet<Rola> Rola { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Rola>()
.HasMany(r => r.Pouzivatelia)
.WithOne(p => p.Rola)
.HasForeignKey(p => p.RolaId);
}
}
Issue: I scaffolded a PouzivatelsController using Visual Studio to generate CRUD operations. When I try to create a user through the Create method, the user is not saved to the database, and it also doesn't show up in the list view. There are no errors in Visual Studio or the logs.
PouzivatelsController (Create method):
// GET: Pouzivatel/Create
public IActionResult Create()
{
ViewData["RolaId"] = new SelectList(_context.Role, "RolaId", "Nazov");
return View();
}
// POST: Pouzivatel/Create
[HttpPost]
[ValidateAntiFeryToken]
public async Task<IActionResult> Create(Pouzivatel pouzivatel)
{
if (ModelState.IsValid)
{
_context.Add(pouzivatel);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
ViewData["RolaId"] = new SelectList(_context.Role, "RolaId", "Nazov", pouzivatel.RolaId);
return View(pouzivatel);
}
Create.cshtml (View):
@model Pouzivatel
<form asp-action="Create">
<div class="form-group">
<label asp-for="Meno" class="control-label"></label>
<input asp-for="Meno" class="form-control" />
<span asp-validation-for="Meno" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Email" class="control-label"></label>
<input asp-for="Email" class="form-control" />
<span asp-validation-for="Email" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="RolaId" class="control-label"></label>
<select asp-for="RolaId" class="form-control" asp-items="ViewBag.RolaId"></select>
<span asp-validation-for="RolaId" class="text-danger"></span>
</div>
<button type="submit" class="btn btn-primary">Create</button>
</form>
Why is the user not being saved to the database? What steps can I take to troubleshoot and fix this issue?
本文标签: cWhy is my ASPNET Core scaffolded Create method not saving data to MySQL databaseStack Overflow
版权声明:本文标题:c# - Why is my ASP.NET Core scaffolded Create method not saving data to MySQL database? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745627825a2159971.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论