I’m writing a custom model binder that will sometimes have to deal with forms. Those forms may have arrays as values.
I’ve been using this method
private static async Task HandleFormAsync(HttpContext httpContext, ModelBindingContext bindingContext)
{
var form = await httpContext.Request.ReadFormAsync();
string jsonString = $"{{{string.Join(",", form.Select(x => $""{x.Key}" : "{x.Value}""))}}}";
try
{
var what = ProcessArray(jsonString);
PagedRequest pagedRequest = what?.ToObject<PagedRequest>();
if (string.IsNullOrEmpty(jsonString))
{
bindingContext.Result = ModelBindingResult.Failed();
return;
}
// Convert the form value to the target model type
var model = JsonConvert.DeserializeObject(jsonString, bindingContext.ModelType);
bindingContext.Result = ModelBindingResult.Success(model);
return;
}
catch (JsonException)
{
// Deserialization failed, possibly due to invalid JSON
bindingContext.Result = ModelBindingResult.Failed();
return;
}
}
It’s not picking up array values though.
The json would be like this for example:
{"filterList[0].filterType" : "contains","filterList[0].filterDataType" : "string","filterList[0].field" : "firstName","filterList[0].filterValue" : "bob","filterList[1].filterType" : "starts_with","filterList[1].filterDataType" : "string","filterList[1].field" : "lastName","filterList[1].filterValue" : "dylan","pageSize" : "15","pageNumber" : "1"}
Would a regex do a better job parsing the array values?
I’ve tried this
["](?<arrayName>w+[d+]).(?<propertyName>w+)["]s*[:]s*["](?<value>.*?)["][,}]
But it’s not matching the first value
Just the bold values
{“filterList[0].filterType” : contains,“filterList[0].filterDataType”:”string”,”filterList[0].field” : “firstName”,”filterList[0].filterValue” : “bob”,”filterList[1].filterType” : “starts_with”,”filterList[1].filterDataType” : “string”,”filterList[1].field” : “lastName”,”filterList[1].filterValue” : “dylan”}
Any ideas how to approach this?