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>
2
Answers
You need to have a new property like
SelectedUserSkillId
inViewModel
and bind this toselect
Also, you need to indicate that to
select
See: Select Tag Helper in ASP.NET Core MVC
Try to add a
List<int>
type property type data toUser
to bind the selected skillids.And then try to change the name of
<select></select>
toSelectedSkillIds
.Because .net core bind data withname
attribute.