【问题标题】:Many-to-many relationship and multi select in asp.net coreasp.net core中的多对多关系和多选
【发布时间】:2018-05-04 07:36:12
【问题描述】:

我正在尝试使用 razor 学习 asp.net 核心,并且我正在尝试创建一个视频游戏数据库来跟踪已完成的游戏、我还没有玩过的游戏等。

但是我有一个问题。我有一张桌子Game 和一张桌子Developer。因为一个游戏可以有很多开发者,而一个开发者可以有很多游戏,所以你制作了第三张桌子DeveloperXGame。 它们是这样的

public class Game
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class Developer
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class DeveloperXGame
{
    public int DeveloperId { get; set; }
    public int JuegoId { get; set; }
    public Developer Developer { get; set; }
    public Game Game { get; set; }
}

public class ApplicationDbContext : DbContext
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) 
        : base(options)
    {

    }

    public DbSet<Game> Game { get; set; }
    public DbSet<Developer> Developer { get; set; }
    public DbSet<DeveloperXGame> DeveloperXGame { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<DeveloperXGame>()
            .HasKey(m => new { m.DeveloperId, m.GameId });
    }
}

我已经为开发人员制作了页面,所以我首先手动创建它们。现在我正在尝试创建游戏,我想显示一个选择,我可以在其中选择该列表中的一个或多个开发人员(如果它们不存在,下一步将尝试通过游戏页面使用 ajax 添加它们) .但我已经迷失了这一点。

我不知道如何加载该列表中的开发者列表,稍后发布如何将这些选定的项目保存在表 DeveloperXGame 中。

谢谢

【问题讨论】:

    标签: entity-framework razor asp.net-core


    【解决方案1】:

    您可以从上下文中删除 public DbSet&lt;DeveloperXGame&gt; DeveloperXGame { get; set; }。

    Index.cshtml.cs
    
    public class IndexModel : PageModel
    {
        private readonly ApplicationDbContext _context;
    
        public IndexModel(ApplicationDbContext context)
        {
            _context = context;
        }
    
        public Game Game { get; set; }
        public IEnumerable<int> Developers { get; set; }
        public IEnumerable<SelectListItem> DeveloperList { get; set; }
        public IActionResult OnGet()
        {
            var developers = from m in _context.Developers
                         select m;
            DeveloperList = developers.Select(m => new SelectListItem { Value = m.Id.ToString(), Text = m.Name });
            return Page();
        }
    }
    

    这是Index.cshtml的视图

    @page
    @model RazorPages.TestGame.Pages.Games.IndexModel
    @{
       ViewData["Title"] = "Index";
    }
    
    <div class="row">
    <div class="col-md-4">
        <form method="post">
            <div class="form-group">
                <label asp-for="Game.Name" class="control-label"></label>
                <input asp-for="Game.Name" class="form-control" />
            </div>
            <div class="form-group">
                <label asp-for="Developers" class="control-label"></label>
                <select asp-for="Developers" asp-items="Model.DeveloperList">
                    <option value="">All</option>
                </select>
            </div>
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </form>
    </div>
    

    您还可以使用 ViewData 在您的视图中传递您的开发者列表。您可以按照官方文档here中的方式浏览此示例网络。希望它可以帮助您入门!

    【讨论】:

    • 谢谢!有用。但是为什么我应该从上下文中删除 public DbSet&lt;DeveloperXGame&gt; DeveloperXGame { get; set; } 呢?当我通过列表将项目添加到数据库时,我不需要在 OnPost 方法上使用它吗?
    • 你可以保留它,但没关系。您可以通过模型访问它们,因为它是多对多的关系! public List&lt;DeveloperGame&gt; DeveloperGames { get; set; } 在你的模型中使用它!
    猜你喜欢
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    • 1970-01-01
    • 2023-02-09
    • 1970-01-01
    • 1970-01-01
    • 2021-10-11
    • 1970-01-01
    相关资源
    最近更新 更多