在尝试了各种方案后,我对如何最好地将下拉列表中的选定值传递回 .cs 中的查询感到困惑。我需要下拉列表的结果成为我的查询中的“官员”值(其中 (x.ProbOfficer == 官员),以便根据官员的姓名和 DateComplete 字段为空的任何记录来过滤报告。
这是.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}";
}
这是我的 .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>
并尝试用 Javascript 来完成它,但知道这是错误的:
<script language="javascript">
{
const dropdown = document.getElementById('SelectOfficer');
dropdown.addEventListener('change', () => {
(SelectOfficer => input.value = officer);
});
}
</script>
任何帮助将不胜感激。我需要在周一之前完成这项工作,但我被困住了!
模型绑定系统通过
name
来绑定属性,无需使用 js 只需更改 name
属性,如下所示:
<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>