I'm fairly new to Razor Pages but I've decided that the simpler programming model looks interesting and have started to modify my projects to the new standard.
I've managed to get other pages to work correctly with the OnGet(), OnPost() and their async versions but I'm receiving an odd error message on a different page.
I've defined the form as below:
<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>
For now I've added a simple submit button to try and debug the issue although ultimately I want to post this form via AJAX.
I've written a few versions of the AJAX code and the above all of which generate the correct POST data and the __RequestVerificationToken but each version creates the same error message which is:
FormatException: Input string was not in a correct format.
System.Number.StringToNumber(ReadOnlySpan<char> str, NumberStyles options, ref NumberBuffer number, NumberFormatInfo info, bool parseDecimal)
The form data being submitted is report by Chrome DevTools as:
Year: Most Recent
Event: 0
Level: 0
__RequestVerificationToken: CfDJ8KjmorZungdGpmSbOzYJHuJ52QtFEFfYGJDh7JmXIfEl9fDT3tXJUVLBSQzam76NWhoSpZLMk0M5ui6dXO9-YjtZ0shVVHWX-iSRchXO3fGiz3iuPQ1RDmenjLIROTwmxFquJRujgQqee_Ay9OGLX5U
The PageModel is using [BindProperties] and contains string fields to receive the incoming POST data as follows:
[Display(Name = "Year:")]
public string Year { get; set; }
[Display(Name = "Event:")]
public string Event { get; set; }
[Display(Name = "Level:")]
public string Level { get; set; }
The issue is being thrown by the Model Binder and is happening before the OnPost() method is fired.
I'll include the full exception text here in case that is helpful:
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
I've been banging my head on this for sometime now and don't seem to be getting anywhere, as I've said I've completed migrations to Razor Pages for other forms submitted via Form or AJAX which work but this one is causing issues.