error in MVC2 on empty string form fields which are required: "This property cannot be set to a null value." - debugger in VS2010 works wrong ?
RSS
[DisplayName("ISBN")]
[Required(ErrorMessage = "ISBN is required.")]
[StringLength(50, ErrorMessage = "ISBN book must have max {1} chars.")]
[DisplayFormat(ConvertEmptyStringToNull = false)]
public string isbn { get; set; }
But when I submit form with empty field ISBN I have an error - ConstraintException:
This property cannot be set to a null value.
I have find that other people have the same problem and they say that debbugger in VS2010 works wrong - yes ?
This is expected behavior. Internally, MVC is setting the model's ISBN property to null. This is triggering EF validation within the property setter and is throwing an exception. MVC swallows this exception and moves on to the next property of the model.
You'll see the debugger activate on this exception if you have Visual Studio set up to break on CLR exceptions (via the menu Debug -> Exceptions). If you're not running under a debugger or don't have the debugger configured to pause on exceptions, you won't
see this exception. Hitting F5 (to continue) from within the debugger will allow program execution to continue as normal.
zielony
Member
207 Points
370 Posts
error in MVC2 on empty string form fields which are required: "This property cannot be set to a n...
Aug 30, 2010 06:30 AM|LINK
In model I have:
[DisplayName("ISBN")] [Required(ErrorMessage = "ISBN is required.")] [StringLength(50, ErrorMessage = "ISBN book must have max {1} chars.")] [DisplayFormat(ConvertEmptyStringToNull = false)] public string isbn { get; set; }But when I submit form with empty field ISBN I have an error - ConstraintException: This property cannot be set to a null value.
I have find that other people have the same problem and they say that debbugger in VS2010 works wrong - yes ?
http://p2p.wrox.com/book-professional-asp-net-mvc-2/79788-constraintexception-unhandled-user-code.html#post259245
http://efreedom.com/Question/1-3129080/Server-Side-Validation-REQUIRED-String-Property-MVC2-Entity-Framework-Work
http://mvcmusicstore.codeplex.com/workitem/6604
levib
Star
7636 Points
1092 Posts
AspNetTeam
Re: error in MVC2 on empty string form fields which are required: "This property cannot be set to...
Aug 30, 2010 07:53 AM|LINK
This is expected behavior. Internally, MVC is setting the model's ISBN property to null. This is triggering EF validation within the property setter and is throwing an exception. MVC swallows this exception and moves on to the next property of the model.
You'll see the debugger activate on this exception if you have Visual Studio set up to break on CLR exceptions (via the menu Debug -> Exceptions). If you're not running under a debugger or don't have the debugger configured to pause on exceptions, you won't see this exception. Hitting F5 (to continue) from within the debugger will allow program execution to continue as normal.
zielony
Member
207 Points
370 Posts
Re: error in MVC2 on empty string form fields which are required: "This property cannot be set to...
Aug 30, 2010 03:34 PM|LINK
ok and I must accept that strange situation or I should do something ?
gabriel.loza...
Contributor
3593 Points
800 Posts
Re: error in MVC2 on empty string form fields which are required: "This property cannot be set to...
Aug 30, 2010 03:51 PM|LINK
What you need to do is before calling UpdateModel() checking whether or not the ModelState is valid:
... if (ModelState.IsValid) { ... UpdateModel(model); ... } ...levib
Star
7636 Points
1092 Posts
AspNetTeam
Re: error in MVC2 on empty string form fields which are required: "This property cannot be set to...
Aug 30, 2010 04:52 PM|LINK
There's nothing to do on your end. The system is working normally.
zielony
Member
207 Points
370 Posts
Re: error in MVC2 on empty string form fields which are required: "This property cannot be set to...
Aug 30, 2010 05:55 PM|LINK
ok so everything works good - good
It is only sad that I can't disable this behaviour - Microsoft should give us this possibility
Nai-Dong Jin...
All-Star
41630 Points
3558 Posts
Re: error in MVC2 on empty string form fields which are required: "This property cannot be set to...
Sep 02, 2010 03:33 AM|LINK
Hi,
>It is only sad that I can't disable this behaviour - Microsoft should give us this possibility
For suggestions, you can submit on connect site. (http://connect.microsoft.com)
Thanks.