Default ASP.NET segregator modelowy MVC 3 nie wiąże właściwości dziesiętnych
Z jakiegoś powodu, kiedy wysyłam ten JSON do akcji:
{"BaseLoanAmount": 5000}
Który ma być powiązany z modelem z właściwością dziesiętną o nazwie "BaseLoanAmount", nie wiąże, po prostu pozostaje 0. Ale jeśli wyślę:
{"BaseLoanAmount": 5000.00}
To wiąże własność, ale dlaczego? Czy 5000 nie może zostać przekonwertowane na zdarzenie dziesiętne, jeśli nie ma liczb dziesiętnych? 46
2 answers
Po wejściu w asp.net kodu źródłowego mvc, wydaje się, że problem polega na tym, że dla konwersji asp.net mvc używa konwertera typów frameworka, który z jakiegoś powodu zwraca false dla konwersji Int do dziesiętnego, skończyło się na użyciu niestandardowego dostawcy segregatora modelu i segregatora modelu dla dziesiętnych, można go zobaczyć tutaj: {]}
public class DecimalModelBinder : DefaultModelBinder
{
#region Implementation of IModelBinder
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
if (valueProviderResult.AttemptedValue.Equals("N.aN") ||
valueProviderResult.AttemptedValue.Equals("NaN") ||
valueProviderResult.AttemptedValue.Equals("Infini.ty") ||
valueProviderResult.AttemptedValue.Equals("Infinity") ||
string.IsNullOrEmpty(valueProviderResult.AttemptedValue))
return 0m;
return Convert.ToDecimal(valueProviderResult.AttemptedValue);
}
#endregion
}
Aby zarejestrować ten ModelBinder, po prostu umieść następującą linię wewnątrz Application_Start()
:
ModelBinders.Binders.Add(typeof(decimal), new DecimalModelBinder());
ModelBinders.Binders.Add(typeof(decimal?), new DecimalModelBinder());
73
Author: ryudice,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-07-19 00:17:48
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-07-19 00:17:48
Spróbuj wysłać to tak:
{ "BaseLoanAmount": "5000" }
12
Author: Darin Dimitrov,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-04-18 05:51:37
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-04-18 05:51:37