skip to Main Content

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


  1. Chosen as BEST ANSWER

    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.


  2. Model Binding system binds the property by name, no need use js just change the name attribute like below:

    <select name="Officer" 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>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search