用asp.net添加带有选择值的N:M表

时间:2019-04-01 21:50:07

标签: c# html asp.net entity-framework

我正在使用ASP.NET Core 2.2开发一种简单的表单,并且在我拥有的表之间有脚本和标签,它们之间具有N:M的关系。我可以在“ CreateScripts.html”文件中的多个标记之间进行选择,但是最后只选择了一个标记。我已经搜索了文档,但找不到答案。谁能帮我?预先感谢。

部分脚本类:

[Key]
public int idScript { get; set; }


[ForeignKey("Tags")]
[Display(Name = "Tags")]
public int idTags { get; set; }

CreateScripts.html选择:

  <div class="form-group">
        <label asp-for="Scripts.idTags" class="control-label"></label>
        </br>
        <select multiple="multiple" asp-for=" Scripts.idTags" asp-items="Model.listatag"> 
        </select>
  </div>

Create.html.cs:

 public async Task OnGet()
        {
            var linguagem = from l in _context.Linguagem
                            select l;
            l = await linguagem.ToListAsync();
            listatag = _context.Tags.Select(x => new SelectListItem
            {
                Value = x.idTags.ToString(),
                Text = x.nmTags
            });             
        }

        [BindProperty]
        public Scripts Scripts { get; set; }

        public async Task<IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return Page();
            }

            Scripts.dtcriacaoScript = DateTime.Today.Date;
            Scripts.dtultimoacessoScript = DateTime.Today.Date;
            Scripts.idVersao = Convert.ToInt32(Scripts.idVersao);
            Scripts.idLinguagem = Convert.ToInt32(Scripts.idLinguagem);
            Scripts.idTags = Convert.ToInt32(Scripts.idTags);

            _context.Scripts.Add(Scripts);
            await _context.SaveChangesAsync();
            return RedirectToPage("./Index");
        }
    }
}

0 个答案:

没有答案