[英]ASP.NET Core 3.0 Razor Pages Select Helper Not Posting Value
I am trying to post back a selected value from a dropdown on an ASP.NET Core 3.0 Razor Pages Page.我正在尝试从 ASP.NET Core 3.0 Razor 页面页面的下拉列表中回发选定的值。 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
.当我发布这些值时,表单的 rest 会通过,但
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).我尝试将类型更改为字符串,以防出现问题,但它只是传入 null 而不是 0。我还验证了该列表已正确加载(当然,您可以看到它由 HTML 正确加载渲染)。
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.我正在运行 Visual Studio 2019 16.4.0 Preview 1.0,该项目是 .NET Core 3.0 项目。
You are using ReportSelect
name for your drop-down while you are trying to bind it to SelectedReportId
without specifying explicit binding.当您尝试将其绑定到
SelectedReportId
而不指定显式绑定时,您正在为下拉列表使用ReportSelect
名称。 Just replace your drop-down name from ReportSelect
to SelectedReportId
and it will resolve your problem.只需将您的下拉名称从
ReportSelect
替换为SelectedReportId
即可解决您的问题。
<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
属性中指定属性名称。
[BindProperty(Name="ReportSelect")]
public int SelectedReportId { get; set; }
Hopefully, It will resolve your problem.希望它会解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.