I'm trying to simply get my selected items populated to the bound property SelectedDataSourceIDs, but I can't figure out what I'm doing wrong.
The view:
@page
@model ContosoUniversity.Pages.Analysis.IndexModel
@{
}
<h1>Analysis</h1>
<form asp-page-handler="Analyze" method="post">
<div class="form-group">
<label asp-for="SelectedDataSourcesIDs" class="control-label"></label>
<select asp-for="SelectedDataSourceIDs" class="form-control" asp-items="@Model.DataSourceNameSL" multiple>
</select>
<span asp-validation-for="SelectedDataSourceIDs" class="text-danger" />
</div>
<div class="form-group">
<input type="submit" value="Analyze" class="btn btn-primary" />
</div>
</form>
The model class:
public class IndexModel : DataSourceResourcesCompaniesSelectListModel
{
private readonly ContosoUniversity.Data.SchoolContext _context;
public IEnumerable<int> SelectedDataSourceIDs { get; set; }
private AnalysisContext _analysisContext;
public IActionResult OnGet()
{
PopulateDataSourceDropDownList(_context);
return Page();
}
public IActionResult OnPost()
{
if (!ModelState.IsValid)
{
return Page();
}
}
}
I have a breakpoint in OnPost and SelectedDataSourceIDs is always null.
What I tried/tested
In
OnGet(), theDataSourceNameSLis correctly populated withDataTextField = "Name"andDataValueField = "ID". I can see and select multiple ListItemsIn the payload of the query, there are the IDs
The naming is correct
The
ModelStateis validIn
OnPost(), theDataSourceNameSLis null andSelectedDataSourceIDsis nullI've even tried changing the Type from
IEnumerable<int>toList<int>
What am I doing wrong?
Add the
[BindProperty]attribute when dealing with model-binding in razor app.