最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Why is my ASP.NET Core scaffolded Create method not saving data to MySQL database? - Stack Overflow

programmeradmin2浏览0评论

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?

发布评论

评论列表(0)

  1. 暂无评论