I'm trying to insert multiple elements chosen by user in <SELECT multiple>
into a SQL Server database .
This is my code:
public class RegisterModel : PageModel
{
private readonly DataAuthDbContext _blogeurContext;
public List<SelectListItem> Options { get; set; }
public class InputModel
{
....
public List<int> FavoriteMovieIds { get; set; }
}
public async Task OnGetAsync(string id, string returnUrl = null)
{
ReturnUrl = returnUrl;
ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
Options = _blogeurContext.TLocalites.Select(a =>
new SelectListItem
{
Value = a.TTerritoireID.ToString(),
Text = a.Nom
}).ToList();
}
......
}
and this is my view
<div class="row">
<label asp-for="Input.FavoriteMovieIds">Type de structure :</label>
<select asp-for="Input.FavoriteMovieIds" class="input-sm Editors"
asp-items="Model.Options" multiple data-placeholder="Ajouter une zone d'intervention"></select>
<span asp-validation-for="Input.FavoriteMovieIds" class="text-danger"></span>
</div>
The list is displaying the data very well, I want to know how to insert the data which the user has chosen into my database
I'm trying this in my post method,
public async Task<IActionResult> OnPostAsync(LocaliteChosie[] l, string? id, string returnUrl = null )
{
....
var zoneIntervention = new TZonesIntervention
{
TIdentificationActeurID = user.Id,
TZonesInterventionId = cleZoneIntervention,
TLocalitesID = item.TLocalitesID,
};
await _blogeurContext.TZoneIntervention.AddAsync(zoneIntervention);
await _blogeurContext.SaveChangesAsync();
}
but it only insert one choice even if the user has chosen several.