剃刀页表单提交返回FormatException:输入字符串的格式不正确

时间:2019-03-06 11:12:28

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

我对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; }

该问题已由Model Binder抛出,并在触发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的迁移,但是可以一个引起问题。

1 个答案:

答案 0 :(得分:0)

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

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

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