Today I tried to put a relatively simple form together and was hoping to be done in a few minutes. Included on the form is an optional field asking for a monetary value. The type in the database is money and using LINQ-to-SQL the type is represented as a decimal.
All was well until the user entered a value that decimals generally don’t like to be converted into. Let’s assume ‘aaa’ for the time being. The result I was greeted with was this:
The model of type 'Foo' was not successfully updated.
Foo foo = _modelRepository.FindFoo(id);UpdateModel(foo);
Foo foo = _modelRepository.FindFoo(id); UpdateModel(foo, "SomeStringField", "SomeOtherStringField"); if (!TryUpdateModel(foo, "TheDecimalField")) { ModelState.AddModelError("TheDecimalField", "Does that look like a number to you?"); }
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.