using System; using System.Globalization; using System.Web.Http.Controllers; using System.Web.Http.ModelBinding; using System.Web.Http.ValueProviders; namespace Kreta.Web.ModelBinder { public class IntegerModelBinder : IModelBinder { public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext) { ValueProviderResult valueResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName); var modelState = new ModelState { Value = valueResult }; bindingContext.ModelState[bindingContext.ModelName] = modelState; if ((valueResult != null) && !(string.IsNullOrWhiteSpace(valueResult.AttemptedValue) && (bindingContext.ModelType == typeof(int?)))) { try { bindingContext.Model = int.Parse(valueResult.AttemptedValue, NumberStyles.Number, CultureInfo.CreateSpecificCulture("hu-HU")); } catch (FormatException e) { modelState.Errors.Add(e); return false; } } return true; } } }