32 lines
1.1 KiB
C#
32 lines
1.1 KiB
C#
using System;
|
|
using System.Globalization;
|
|
using System.Web.Mvc;
|
|
|
|
namespace Kreta.Web.ModelBinder.Mvc
|
|
{
|
|
public class DoubleModelBinder : IModelBinder
|
|
{
|
|
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
|
|
{
|
|
ValueProviderResult valueResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
|
|
var modelState = new ModelState { Value = valueResult };
|
|
bindingContext.ModelState[bindingContext.ModelName] = modelState;
|
|
|
|
object actualValue = null;
|
|
|
|
if ((valueResult != null) && !(string.IsNullOrWhiteSpace(valueResult.AttemptedValue) && (bindingContext.ModelType == typeof(double?))))
|
|
{
|
|
try
|
|
{
|
|
actualValue = double.Parse(valueResult.AttemptedValue, NumberStyles.Number, CultureInfo.CreateSpecificCulture("hu-HU"));
|
|
}
|
|
catch (FormatException e)
|
|
{
|
|
modelState.Errors.Add(e);
|
|
}
|
|
}
|
|
|
|
return actualValue;
|
|
}
|
|
}
|
|
}
|