2021年3月28日星期日

Why does my List of Objects rendered as Checkboxes return an Empty List? [ASP.NET Core - 3.1]

I'm trying to render a List<PlayerCheckbox> as Checkboxes.

PlayerCheckbox looks like this:

 public class PlayerCheckbox      {          public PlayerCheckbox(string discordName, ulong discordId, bool selected = false)          {              DiscordName = discordName;              DiscordId = discordId;              Selected = selected;          }            public string DiscordName { get; set; }            public ulong DiscordId { get; set; }            public bool Selected { get; set; }      }  

The CreateBingoTaskModel looks like this: (ViewModel I am working with on the Create page)

 public class CreateBingoTaskModel      {          /// <summary>          /// Discord Id of the creator of this BingoTask          /// </summary>          public BingoPlayer Creator { get; set; }            [Range(0, 100)]          public int Difficulty { get; set; }            /// <summary>          /// List of IDs that are not allowed to receive this bingo task          /// </summary>          public List<BingoPlayer> BlackListIds { get; set; } = new List<BingoPlayer>();            /// <summary>          /// Title of this bingo task          /// </summary>          [Required]          public string BingoTitle { get; set; }            /// <summary>          /// Description of this bingo task          /// </summary>          [Required]          public string BingoDescription { get; set; }            public DateTime RaffledOn { get; set; }            public List<PlayerCheckbox> BlackList;      }  

The setup GET-Create looks like this:

public async Task<IActionResult> Create()          {              CreateBingoTaskModel createBingoModel = new CreateBingoTaskModel();              createBingoModel.BlackList = new List<PlayerCheckbox>();                var user = await appDb.BingoPlayers.SingleOrDefaultAsync(player =>                  player.LoginName == HttpContext.Session.GetString("user"));                foreach (BingoPlayer bp in appDb.BingoPlayers)              {                  PlayerCheckbox playerCheckbox =                      new PlayerCheckbox(bp.Name, bp.DiscordId);                  if (playerCheckbox.DiscordId != user.DiscordId)                  {                      createBingoModel.BlackList.Add(playerCheckbox);                  }              }                return View(createBingoModel);          }  

Looking at the Model in the POST-Create Header: PostCreate Model Screenshot shows that the BlackList is now null. Great. Why the hell.

The Checkbox List Code I am using looks like this:

<div class="form-group">                  <span>BlackList</span>                    @for (int i = 0; i < Model.BlackList.Count; i++)                  {                      <input hidden asp-for="@Model.BlackList[i].DiscordId" />                      <input hidden asp-for="@Model.BlackList[i].DiscordName" />                      <input asp-for="@Model.BlackList[i].Selected" type="checkbox" />                      @Model.BlackList[i].DiscordName                      <br />                  }  </div>  

and is taken from this answer here: https://forums.asp.net/t/2164304.aspx?Asp+net+MVC+Core+giving+null+checkbox+data+instead+of+all+selected+checkboxes+on+HTTPPost

Many implementations of CheckboxLists have extremely similar frontend code with the only major change being a replacement of for with foreach(playerCheckbox in BlackList) which I have tried too.

My FormData looks... fine? (Except the extra values at the bottom) enter image description here

I do NOT understand at all why the binding doesn't work here. Am I so tired that I am overlooking somethingy extremely simple?

https://stackoverflow.com/questions/66847626/why-does-my-list-of-objects-rendered-as-checkboxes-return-an-empty-list-asp-ne March 29, 2021 at 09:07AM

没有评论:

发表评论