ASP.net 多个 select 总是 returns 0 select 个项目

ASP.net multiple select always returns 0 selected items

所以我正在尝试构建一个用户多 select 以便让参与者参与到一个项目中。这是我的代码:

  <form asp-page-handler="ProjectModalPartial">
                @*numele metodei*@
                <input name="IsValid" type="hidden" value="@ViewData.ModelState.IsValid.ToString()" />
                <div class="form-group">
                    <label asp-for="ProjectsModel.ProjectName">Title</label>
                    <input asp-for="ProjectsModel.ProjectName" class="form-control" placeholder="MyProject1" />
                    <span asp-validation-for="ProjectsModel.ProjectName" class="text-danger"></span>
                </div>
                <br />
                <div class="form-group">
                    <label asp-for="ProjectsModel.ProjectDescription">Description</label>
                    <textarea asp-for="ProjectsModel.ProjectDescription" class="form-control" rows="3" placeholder="This is my first project"></textarea>
                    <span asp-validation-for="ProjectsModel.ProjectDescription" class="text-danger"></span>
                </div>
                <br />

                <div class="form-group">

                    <label asp-for="ProjectsModel.MultiValues">Project participants</label>
                    <select multiple="multiple" class="form-control chosen_participants" id="listbox1" asp-for="ProjectsModel.MultiValues" asp-items="@Model.Items" style="width: 470px;">
                     
                    </select>
                    <span asp-validation-for="ProjectsModel.MultiValues" class="text-danger"></span>

                </div>
            </form>

页面:

 public ApplicationDbContext _context;

    public ProjectsList(ApplicationDbContext context)
    {
        _context = context;
    }

    public List<Project> ProjectList { get; private set; } = new List<Project>();
    [BindProperty]
    public ProjectsModel ProjectsModel { get; set; }
    public List<User> UserList { get; private set; } = new List<User>();

    public IEnumerable<SelectListItem> Items { get; set; }
    = new List<SelectListItem>();
    public void OnGet()
    {

        ProjectList =  _context.Projects.ToList();
        Items = (from usr in this._context.Users
                 select new SelectListItem
                 {
                     Text = usr.Email.ToString(),
                     Value = usr.Id.ToString()
                 }).ToList();


    }

    public IActionResult OnPostProjectModalPartial()
    {
        Items = (from usr in this._context.Users
                 select new SelectListItem
                 {
                     Text = usr.Email.ToString(),
                     Value = usr.Id.ToString()
                 }).ToList();

        if (ModelState.IsValid)
        {
            var project = new Project
            {
                ProjectName = ProjectsModel.ProjectName,
                ProjectDescription = ProjectsModel.ProjectDescription,
                Created = DateTime.Now,
                Active = true,
                ProjectCreatorId = User.FindFirst(ClaimTypes.NameIdentifier).Value

            };
            _context.Projects.Add(project);
            _context.SaveChanges();
            foreach (var person in Items)
            {
                _context.ProjectParticipants.Add(new ProjectParticipant
                {
                    ProjectId = project.Id,
                    UserId = person.Value
                });


            }
            _context.SaveChanges();
        }
        return Page();
    }

和模型:

   public class ProjectsModel
{
    public int Id { get; set; }
    [Required]
    public string ProjectName { get; set; }
    [Required]
    public string ProjectDescription { get; set; }
    public DateTime Created { get; set; }
    public bool Active { get; set; }

    public string ProjectCreatorId { get; set; }

    [Required(ErrorMessage = "You must select atleast one participant")]
    public List<User> ProjectParticipants { get; set; }

    public IEnumerable<string> MultiValues { get; set; }


}

但是,当我在 OnPostProjectModalPartial 的 foreachloop 中放置断点时,我可以看到列表中填充了所有用户,但其中 none 是 selected。我究竟做错了什么? 我需要一些 jquery 脚本来完成这项工作吗?

如果你想选择一些用户列表你需要更改你的代码,这段代码在页面部分

原版

foreach (var person in Items)
{
    _context.ProjectParticipants.Add(new ProjectParticipant
    {
         ProjectId = project.Id,
         UserId = person.Value
     });
} //ORIGINAL

进入这个: 仅过滤 Model.Iems selected

foreach (var person in model.Items.Where(item=> item.Selected))
{
    _context.ProjectParticipants.Add(new ProjectParticipant
    {
         ProjectId = project.Id,
         UserId = person.Value
     });
}

此外,您不需要再次从数据库中获取列表,您已经在模型中获取了它

您需要指明与所选选项关联的模型的属性:

<select multiple="multiple" 
   class="form-control chosen_participants" 
   id="listbox1" 
   asp-for="UserId" // <-- This property
   asp-items="@Model.Items" 
   style="width: 470px;">

并将 UserId 属性 添加到您的 class:

public int UserId { get; set; }

然后,在 post 中,您的模型 UserId 具有选定的用户 ID,您可以进行查询以获取您需要的有关该用户的所有信息。

注意:在您当前的 OnPostProjectModalPartial 中,您正在数据库中进行查询,但没有从 post.

中获取任何类型的选择信息