ASP.NET Core 2.0 MVCを初めて使用しているので、PlayerのタイプのICollectionを持つ新しいタイプのエンティティを追加するデモアプリケーションがありますそのモデルで。別のエンティティのコレクションを使用してエンティティを作成する方法
関与するクラスです
public class Lineup
{
public int LineupID { get; set; }
public int PlayerID { get; set; }
public ICollection<Player> Attendants { get; set; }
}
と
public class Player
{
public int PlayerID { get; set; }
public string Name { get; set; }
public string Email { get; set; }
}
私は、ラインナップのビューを作成し、この
<form asp-action="Create">
<div class="form-horizontal">
<h4>Lineup</h4>
<hr />
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Attendants" class="col-md-2 control-label"></label>
<div class="col-md-10">
<select asp-for="Attendants" class="form-control"
asp-items="ViewBag.Players" multiple="multiple">
</select>
</div>
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
</form>
ようViewBag.Playersに見えるLineupsControllerの一部です:
それは、これまでどのように見えるかだ:上記のサンプルから予想されるよう
が、私の作成方法
public async Task<IActionResult> Create([Bind("Attendants")] Lineup lineup)
{
if (ModelState.IsValid)
{
_context.Add(lineup);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(lineup);
}
内アテンダントがラインナップエンティティのカウントが0ではなく2であります。ここで何が間違っていますか?助けてくれてありがとう。
は、このリンクには、[OK]が、その0、NULLでないhttps://www.mikesdotnetting.com/article/265/asp-net-mvc-dropdownlists-multiple-selection-and-enum-support – Rohit