I am trying to post back a selected value from a dropdown on an ASP.NET Core 3.0 Razor Pages Page. I am getting back the default value every time. I can't see what I am missing.
Here is the code:
Page:
<select asp-for="SelectedReportId" class="form-control" name="ReportSelect"
asp-items="@(new SelectList(Model.Reports, nameof(ReportsModel.Id), nameof(ReportsModel.ReportName)))">
</select>
Code-behind:
[BindProperty]
public int SelectedReportId { get; set; }
That renders as this on the form:
<select class="form-control" name="ReportSelect" data-val="true" data-val-required="The SelectedReportId field is required." id="SelectedReportId">
<option value="1">All People</option>
<option value="2">People Starting With T</option>
</select>
When I post the values, the rest of the form comes through but nothing comes through for SelectedReportId
. I tried changing the type to string, in case that was an issue but then it just passes in null instead of 0. I also verified that the list was properly being loaded (of course, you can see that it loads by the HTML being properly rendered).
I tried about every configuration that I could think of without any success. I even cloned the project down to another machine, just to test it out there, but I got the same results.
I am running Visual Studio 2019 16.4.0 Preview 1.0 and the project is a .NET Core 3.0 project.
You are using ReportSelect
name for your drop-down while you are trying to bind it to SelectedReportId
without specifying explicit binding. Just replace your drop-down name from ReportSelect
to SelectedReportId
and it will resolve your problem.
<select asp-for="SelectedReportId" class="form-control" name="SelectedReportId"
asp-items="@(new SelectList(Model.Reports, nameof(ReportsModel.Id),nameof(ReportsModel.ReportName)))">
</select>
Or if you don't want to change your drop-down name then you need to specify property name in your BindProperty
attribute.
[BindProperty(Name="ReportSelect")]
public int SelectedReportId { get; set; }
Hopefully, It will resolve your problem.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.