简体   繁体   English

ASP.NET 核心 3.0 Razor 页面 Select 帮助程序未发布值

[英]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.

相关问题 ASP.NET Core 3.0 Razor 页面中的路由本地化 - Routed localization in ASP.NET Core 3.0 Razor Pages ASP.NET Core with Razor Pages: 从 DropDownList 返回值 - ASP.NET Core with Razor Pages: Return value from DropDownList ASP.NET Core Razor页面的路由 - Routing for ASP.NET Core Razor Pages Razor Pages 中的 Asp.net 核心本地化 - Asp.net core Localization in Razor Pages 如何在 ASP.NET Core Razor Pages 部分标签助手中动态传递名称属性? - How to pass name attribute dynamically in ASP.NET Core Razor Pages partial tag helper? Asp.net Core 3.0 WebApi 和 MVC 的单独全局异常处理(包括 Razor 页面) - Asp.net Core 3.0 Separate Global Exception Handling for WebApi and MVC (including Razor Pages) 如何在单个应用程序中处理多个域? ASP.net 核心 3.0(剃须刀页面,天蓝色) - How to work with mulitple domains in a single app? ASP.net core 3.0 (razor pages, azure) Razor Page,如何使用标签助手 asp.net core 进行路由 - Razor Page, How to routing with tag helper asp.net core 重命名 ASP.NET 核心 Razor 页面中的页面/共享目录 - Rename Pages/Shared directory in ASP.NET Core Razor Pages Asp.Net Core Razor Pages: asp-route to use value from input on the same page - Asp.Net Core Razor Pages: asp-route to use value from input on same page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM