Home > Software engineering >  Asp.Net Core MultiSelect SelectList Post Problem
Asp.Net Core MultiSelect SelectList Post Problem

Time:11-24

When I submit the form, the user skills come. I use many to many relationship but always come count 0 on post action

public class User : IEntity
{
    [Key]
    public int id { get; set; }
    public Guid userID { get; set; }
    public string mongoUserID { get; set; }
    public string username { get; set; }
    public string password { get; set; }
    public string repassword { get; set; }
    public string role { get; set; }
    public string name { get; set; }
    public string surname { get; set; }
    public string email { get; set; }
    public string phone { get; set; }
    public string title { get; set; }
    public string education { get; set; }
    public string location { get; set; }
    public DateTime? birthday { get; set; }
    public string image { get; set; } = "user.png";
    public bool isActive { get; set; } = true;
    public DateTime createdAt { get; set; } = DateTime.Now;
    public DateTime? updatedAt { get; set; }
    public IList<UserSkill> UserSkills { get; set; }
}

public class Skill : IEntity
{
    [Key]
    public int id { get; set; }
    public Guid skillID { get; set; }
    public string mongoSkillID { get; set; }
    public string name { get; set; }
    public bool isactive { get; set; } = true;
    public DateTime createdAt { get; set; } = DateTime.Now;
    public DateTime? updatedAt { get; set; }
    public IList<UserSkill> UserSkills { get; set; }
}

public class UserSkill
{
    public int UserId { get; set; }
    public User User { get; set; }
    public int SkillId { get; set; }
    public Skill Skill { get; set; }
}

[HttpGet]
public async Task<IActionResult> adduser()
{
    var skills = await _skillService.GetListByFilter();
    if (skills.Count > 0)
    {
        ViewBag.Skills = skills.Select(x => new SelectListItem()
        {
            Value = x.id.ToString(),
            Text = x.name
        });
    }
    return View();
}
[HttpPost]
public async Task<IActionResult> adduser(User user)
{
    
}

<div class="form-group col-md-6">
     <label>User Skills</label>
     <select name="UserSkills" asp-for="UserSkills" asp-items="@(new SelectList(ViewBag.Skills,"Value", "Text"))" class="form-control select2" multiple="multiple" data-placeholder="Select a Skills" style="width: 100%;"></select>
</div>

CodePudding user response:

Try to add a List<int> type property type data to User to bind the selected skillids.

public class User : IEntity
{
    ...
    public List<int> SelectedSkillIds { get; set; }
}

And then try to change the name of <select></select> to SelectedSkillIds.Because .net core bind data with name attribute.

<div class="form-group col-md-6">
     <label>User Skills</label>
     <select name="SelectedSkillIds" asp-items="@(new SelectList(ViewBag.Skills,"Value", "Text"))" class="form-control select2" multiple="multiple" data-placeholder="Select a Skills" style="width: 100%;"></select>
</div>

CodePudding user response:

You need to have a new property like SelectedUserSkillId in ViewModel and bind this to select

public class User: IEntity
{
    [Key]
    public int id { get; set; }
    ...
    public IList<UserSkill> UserSkills { get; set; }
    public List<string> SelectedUserSkillIds { get; set; } //--> Add a new property
}

Also, you need to indicate that to select

<div class="form-group col-md-6">
     <label>User Skills</label>
     <select name="SelectedUserSkillIds" asp-for="SelectedUserSkillIds" asp-items="@(new SelectList(ViewBag.Skills,"Value", "Text"))" class="form-control select2" multiple="multiple" data-placeholder="Select a Skills" style="width: 100%;"></select>
</div>

See: Select Tag Helper in ASP.NET Core MVC

  • Related