-
Notifications
You must be signed in to change notification settings - Fork 34
/
BoolBinder.cs
40 lines (33 loc) 路 1.15 KB
/
BoolBinder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
锘縰sing System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.ModelBinding;
namespace Tzkt.Api
{
public class BoolBinder : IModelBinder
{
public Task BindModelAsync(ModelBindingContext bindingContext)
{
var model = bindingContext.ModelName;
var hasValue = false;
if (!bindingContext.TryGetBool($"{model}", ref hasValue, out var value))
return Task.CompletedTask;
if (!bindingContext.TryGetBool($"{model}.eq", ref hasValue, out var eq))
return Task.CompletedTask;
if (!bindingContext.TryGetBool($"{model}.null", ref hasValue, out var isNull))
return Task.CompletedTask;
if (!hasValue)
{
bindingContext.Result = ModelBindingResult.Success(null);
return Task.CompletedTask;
}
bindingContext.Result = ModelBindingResult.Success(new BoolParameter
{
Eq = value ?? eq,
Null = isNull
});
return Task.CompletedTask;
}
}
}