I have web api net framework 4.7 and I made a custom modelbinder that converts decimal numbers to integer. When they call my product post I have cases that come to me with 5.00. I want to take the integer part of the number as 5.On the other hand if they send me 5.01 I discard the value for this case. the signature of the method is like this
public HttpResponseMessage Post([ModelBinder(typeof(DecimalToIntModelBinder))] [FromBody] IEnumerable<Product> products)
thee Product class has a Stock class inside with the int?Quantity property that I want to take the integer part as explained before
I did the model binder like this
public class DecimalToIntModelBinder : IModelBinder
{
public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
{
if (bindingContext.ModelType == typeof(int?))
{
var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
if (value != null && decimal.TryParse(value.AttemptedValue, out decimal result) && result % 1 == 0)
{
bindingContext.Model = (int)result;
return true;
}
}
return false;
}
}
I do this mentioned before and I get an error "Cannot bind parameter 'products' because it has attributes that conflict. I wanted to do the same for attributes on the property but it didn't work for me. Do you have any ideas or suggestions on how to make this custom binder.?
Regards