I am confused, having tried various scenarios, on how best to pass a selected value from a dropdown list back to my query in my .cs. I need the results of the dropdown to become the ‘officer’ value in my query here (where (x.ProbOfficer == officer), so that the report is filtered by the officer’s name and any records where the DateComplete field is empty.
Here is the .cs:
[BindProperties]
public class IndexModel : PageModel
{
private readonly ApplicationDbContext _db;
public IndexModel(ApplicationDbContext db)
{
_db = db;
}
public IEnumerable<Officer> DisplayProbOfficerData { get; set; }
public IEnumerable<InvestigationLog> Results { get; set; }
public async Task OnGetAsync()
{
await _db.Officer.Select(a => a.DisplayName).ToListAsync();
DisplayProbOfficerData = await _db.Officer.ToListAsync();
Results = _db.InvestigationLog.ToList();
}
public void OnPost(string officer)
{
Results = (from x in _db.InvestigationLog where (x.ProbOfficer == officer) && (x.DateComplete.ToString() == "") select x).ToList();
var officer1 = officer;
ViewData["officerparameter"] = $"For Officer: {officer1}";
}
And here is the pertinent portion of my .cshtml:
<form asp-page="./Index" method="post">
<div class="form-actions no-color">
<p>
<h5>Probation Officer Selection</h5>
<br/>
<select name="SelectOfficer" id="Select1" class="form-select" asp-items="@(new SelectList(Model.DisplayProbOfficerData.OrderBy(x => x.DisplayName),"DisplayName", "DisplayName"))"><option value="" selected disabled>---Select Probation Officer---</option></select>
<br/>
<input type="submit" value="Filter Report" class="btn btn-primary mx-2" />
<a asp-page="./Index" class="btn btn-link mx-2">Return to Full List</a>
</p>
</div>
And trying Javascript to get it done, but know it’s wrong:
<script language="javascript">
{
const dropdown = document.getElementById('SelectOfficer');
dropdown.addEventListener('change', () => {
(SelectOfficer => input.value = officer);
});
}
</script>
Any assistance would be greatly appreciated. I need this done by Monday and am stuck!
2
Answers
Well, this one was on me. Come to find out that in the data, any time there was a "blank" data complete, the database was actually storing the date 01/01/1900. Once I set those to null, the report returned the data I wanted.
Model Binding system binds the property by
name
, no need use js just change thename
attribute like below: