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.
My desire for ASP.NET MVC to handle this for me and put a nice validation message into the ModelState was just not happening.
My original code did this:
Foo foo = _modelRepository.FindFoo(id);The relatively simple fix is:
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?");
}
This is a nice article..
ReplyDeleteIts easy to understand ..
And this article is using to learn something about it..
c#, dot.net, php tutorial, Ms sql server
Thanks a lot..!
ri70