init
This commit is contained in:
commit
e124a47765
19374 changed files with 9806149 additions and 0 deletions
32
KretaWeb/ModelBinder/IntegerModelBinder.cs
Normal file
32
KretaWeb/ModelBinder/IntegerModelBinder.cs
Normal file
|
@ -0,0 +1,32 @@
|
|||
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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue