【问题标题】:Razor Pages Form Submit Returns FormatException: Input string was not in a correct formatRazor Pages 表单提交返回 FormatException:输入字符串的格式不正确
【发布时间】:2019-07-28 00:52:24
【问题描述】:

我对 Razor Pages 还很陌生,但我认为更简单的编程模型看起来很有趣,并开始将我的项目修改为新标准。

我已经设法让其他页面与 OnGet()、OnPost() 及其异步版本一起正常工作,但我在不同的页面上收到一条奇怪的错误消息。

我已经定义了如下形式:

    <form method="post" id="coursepictures-form" role="form">
        <div class="form-row">
            <div class="form-group col-lg-2">
                <label asp-for="Year" class="form-control-label mb-1 mr-1"></label>
                <select asp-for="Year" class="form-control form-control-sm mb-1 mr-3" asp-items="@(new SelectList(Model.YearList))"></select>
            </div>

            <div class="form-group col-lg-5">
                <label asp-for="Event" class="form-control-label mb-1 mr-1"></label>
                <select asp-for="Event" class="form-control form-control-sm mb-1 mr-3" asp-items="@(new SelectList(Model.EventList, "Key", "Value"))"></select>
            </div>

            <div class="form-group col-lg-4">
                <label asp-for="Level" class="form-control-label mb-1 mr-1"></label>
                <select asp-for="Level" class="form-control form-control-sm mb-1 mr-3" asp-items="@(new SelectList(Model.LevelList, "Key", "Value"))"></select>
            </div>
            <button>Submit</button>
        </div>
    </form>

现在我添加了一个简单的提交按钮来尝试调试问题,尽管最终我想通过 AJAX 发布此表单。

我已经编写了几个版本的 AJAX 代码,以上所有这些都生成正确的 POST 数据和 __RequestVerificationToken 但每个版本都会创建相同的错误消息:

FormatException: Input string was not in a correct format.
System.Number.StringToNumber(ReadOnlySpan<char> str, NumberStyles options, ref NumberBuffer number, NumberFormatInfo info, bool parseDecimal)

提交的表单数据由 Chrome DevTools 报告为:

Year: Most Recent
Event: 0
Level: 0
__RequestVerificationToken: CfDJ8KjmorZungdGpmSbOzYJHuJ52QtFEFfYGJDh7JmXIfEl9fDT3tXJUVLBSQzam76NWhoSpZLMk0M5ui6dXO9-YjtZ0shVVHWX-iSRchXO3fGiz3iuPQ1RDmenjLIROTwmxFquJRujgQqee_Ay9OGLX5U

PageModel 使用 [BindProperties] 并包含用于接收传入 POST 数据的字符串字段,如下所示:

    [Display(Name = "Year:")]
    public string Year { get; set; }

    [Display(Name = "Event:")]
    public string Event { get; set; }

    [Display(Name = "Level:")]
    public string Level { get; set; }

问题是由模型绑定器引发的,并且在 OnPost() 方法被触发之前发生。

如果有帮助,我将在此处包含完整的异常文本:

System.Number.StringToNumber(ReadOnlySpan<char> str, NumberStyles options, ref NumberBuffer number, NumberFormatInfo info, bool parseDecimal)
System.Number.ParseInt32(ReadOnlySpan<char> s, NumberStyles style, NumberFormatInfo info)
int.Parse(string s, NumberStyles style, IFormatProvider provider)
System.ComponentModel.Int32Converter.FromString(string value, NumberFormatInfo formatInfo)
System.ComponentModel.BaseNumberConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
Microsoft.AspNetCore.Mvc.ModelBinding.Internal.ModelBindingHelper.ConvertSimpleType(object value, Type destinationType, CultureInfo culture)
Microsoft.AspNetCore.Mvc.ModelBinding.Internal.ModelBindingHelper.UnwrapPossibleArrayType(object value, Type destinationType, CultureInfo culture)
Microsoft.AspNetCore.Mvc.ModelBinding.Internal.ModelBindingHelper.ConvertTo(object value, Type type, CultureInfo culture)
Microsoft.AspNetCore.Mvc.ModelBinding.Binders.DictionaryModelBinder<TKey, TValue>.BindModelAsync(ModelBindingContext bindingContext)
Microsoft.AspNetCore.Mvc.ModelBinding.ParameterBinder.BindModelAsync(ActionContext actionContext, IModelBinder modelBinder, IValueProvider valueProvider, ParameterDescriptor parameter, ModelMetadata metadata, object value)
Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageBinderFactory+<>c__DisplayClass2_0+<<CreatePropertyBinder>g__Bind|0>d.MoveNext()
Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker.BindArgumentsCoreAsync()
Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker.InvokeInnerFilterAsync()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke(HttpContext httpContext)
Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.Invoke(HttpContext httpContext)
Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
ShoestringEventing.Startup+<>c+<<Configure>b__5_0>d.MoveNext() in Startup.cs

我一直在努力解决这个问题,但似乎没有取得任何进展,正如我所说,我已经完成了通过 Form 或 AJAX 提交的其他表单的迁移到 Razor Pages 的工作,但是这个一个是导致问题。

【问题讨论】:

    标签: asp.net-core-mvc razor-pages


    【解决方案1】:

    看起来我一直在使用 [BindProperties] 是一种相当懒惰的方式,我应该只绑定来自表单的值。

    在这种情况下,PageModel 中还有其他条目在 OnGet() 和 OnPost() 处理程序中使用,但不是表单数据的一部分,这些条目会引发错误。

    进行此更改后,OnPost() 代码会正确触发。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-10
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      相关资源
      最近更新 更多