[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.
So after a long day of trying to figure out what went wrong here in the project I am working on.
This happens when you are binding a Data Member from an Entity Framework model where the field in the DB is set to not allow nulls (Not Null), that gets bound to a form field in the UI via a strongly typed view. This in turn causes the Entity Object in your
model to not allow nulls. The view engine attempts to do a prebind from your form to the model object and this is where everything goes pear shaped. Apparently entity framework will convert an emtpy string to null coming from an input form. This is where and
why the constraint exception happens.
To over come this You can create a MetaData Class that holds the fields you are having the issue with, or any other fields you wish to validate via jquery unobtrusive validation. You would then decorate the Parameters in the MetaData class with the Data
Annotation DisplayFormat(ConvertEmptyStringToNull = false), this will cause the field decorated by this to not convert that empty into a null which will allow the code not to throw the exception. This fixes the debugger issue. The last thing is to decorate
the field with the required annotation so that the validation picks it up prior to the post.
Where EntityClass is an actual Entity in your Data Model
public class EntityClassMetaData {
[Required(ErrorMessage="Your message goes here")]
[DisplayFormat(ConvertEmptyStringToNull = false)]
public String FieldToValidate;
...
}
You would then create a partial class of the EntityClass that you built the Meta Data Class for. The partial class can be empty since you are not trying to over ride any functionality in the Entity Class, you are simply trying to add functionality. That
partial class will be wrapped up by the framework and compiled into one big class with all of the other partials of the same class. Once the partial is created you can then decorate the Entity Class with a MetaDataType annotation like so:
[MetaDataType(typeof(EntityClassMetaData))]
public partial class EntityClass {
// Empty Class
}
This will then ensure that your validation picks up and treats the field the way you intend it to be treated without throwing an error either where you notice it, or in the background.
The issue I have with many of the thoughts on this issue to just turn off the exception break in the debugger is this: That solves nothing of the underlying problem. At least this avenue will will get you closer to the correct way of fixing this issue.
zielony
Member
227 Points
426 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
7702 Points
1099 Posts
Microsoft
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
227 Points
426 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
3583 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
7702 Points
1099 Posts
Microsoft
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
227 Points
426 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.
stieferj
Member
4 Points
7 Posts
Re: error in MVC2 on empty string form fields which are required: "This property cannot be set to...
Jul 18, 2012 08:58 PM|LINK
So after a long day of trying to figure out what went wrong here in the project I am working on.
This happens when you are binding a Data Member from an Entity Framework model where the field in the DB is set to not allow nulls (Not Null), that gets bound to a form field in the UI via a strongly typed view. This in turn causes the Entity Object in your model to not allow nulls. The view engine attempts to do a prebind from your form to the model object and this is where everything goes pear shaped. Apparently entity framework will convert an emtpy string to null coming from an input form. This is where and why the constraint exception happens.
To over come this You can create a MetaData Class that holds the fields you are having the issue with, or any other fields you wish to validate via jquery unobtrusive validation. You would then decorate the Parameters in the MetaData class with the Data Annotation DisplayFormat(ConvertEmptyStringToNull = false), this will cause the field decorated by this to not convert that empty into a null which will allow the code not to throw the exception. This fixes the debugger issue. The last thing is to decorate the field with the required annotation so that the validation picks it up prior to the post.
Where EntityClass is an actual Entity in your Data Model
public class EntityClassMetaData {
[Required(ErrorMessage="Your message goes here")]
[DisplayFormat(ConvertEmptyStringToNull = false)]
public String FieldToValidate;
...
}
You would then create a partial class of the EntityClass that you built the Meta Data Class for. The partial class can be empty since you are not trying to over ride any functionality in the Entity Class, you are simply trying to add functionality. That partial class will be wrapped up by the framework and compiled into one big class with all of the other partials of the same class. Once the partial is created you can then decorate the Entity Class with a MetaDataType annotation like so:
[MetaDataType(typeof(EntityClassMetaData))]
public partial class EntityClass {
// Empty Class
}
This will then ensure that your validation picks up and treats the field the way you intend it to be treated without throwing an error either where you notice it, or in the background.
The issue I have with many of the thoughts on this issue to just turn off the exception break in the debugger is this: That solves nothing of the underlying problem. At least this avenue will will get you closer to the correct way of fixing this issue.