The DefaultModelBinder.cs BindProperty Method calls GetPropertyValue (line 200) method which returns null for string.Empty if ConvertEmptyStringToNull = true (line 400), and ConvertEmptyStringToNull is true by default
in ModelMetadata.cs (line 18) for everything not overridden with an attribute.
In MVC 1.0, the BindProperty just used propertyBinder.BindModel(controllerContext, innerBindingContext); (line 217) which did not coerce.
I think the new behaviour is correct, but there should be a global way to turn it off. Maybe have the ModelMetadata expose a static property that all instances are initialized to.
I agree that you can set the value to 'false'. However the set to true is done in the MetaData constructor, and the MetaData objects are created internally by the ModelBinder for each property. This means implementing your own MetaData+MetaDataProvider to
set to true or implementing your own ModelBinder to ignore the check. A simple static property to intialize would be better and clearer.
I agree that you can set the value to 'false'. However the set to true is done in the MetaData constructor, and the MetaData objects are created internally by the ModelBinder for each property. This means implementing your own MetaData+MetaDataProvider to
set to true or implementing your own ModelBinder to ignore the check. A simple static property to intialize would be better and clearer.
Brian
Hi Brian,
I know what is your thinking now.
But we can not change this behaviour, the Qualifier of class of Parameter is not
'virtual',
We will not be changing the default value of this, because it lines up with the expectations of not only previous frameworks (WebForms treating empty strings as null by default), but also other platforms (f.e., RIA Services).
Use the [DisplayFormat] attribute to override this on a case-by-case basis.
Marked as answer by ricka6 on Feb 15, 2010 10:43 PM
bchance
Member
8 Points
19 Posts
MVC 2 RC + ModelMetaData.ConvertEmptyStringToNull + ModelBinder
Feb 04, 2010 06:52 PM|LINK
Any way we can make ConvertEmptyStringToNull = false by default? Or at least globally override?
Having all "" convert to null is a breaking change in behavior from 1.0 and is causing havoc with my upgrade.
Brian
ali62b
Contributor
4750 Points
690 Posts
Re: MVC 2 RC + ModelMetaData.ConvertEmptyStringToNull + ModelBinder
Feb 05, 2010 10:44 AM|LINK
Check out this link :http://weblogs.asp.net/scottgu/archive/2010/02/05/asp-net-mvc-2-release-candidate-2-now-available.aspx#7335401
@BlueCoder
Regards
bchance
Member
8 Points
19 Posts
Re: MVC 2 RC + ModelMetaData.ConvertEmptyStringToNull + ModelBinder
Feb 05, 2010 01:42 PM|LINK
Unfortunately RC2 does not fix the issue.
The DefaultModelBinder.cs BindProperty Method calls GetPropertyValue (line 200) method which returns null for string.Empty if ConvertEmptyStringToNull = true (line 400), and ConvertEmptyStringToNull is true by default in ModelMetadata.cs (line 18) for everything not overridden with an attribute.
In MVC 1.0, the BindProperty just used propertyBinder.BindModel(controllerContext, innerBindingContext); (line 217) which did not coerce.
I think the new behaviour is correct, but there should be a global way to turn it off. Maybe have the ModelMetadata expose a static property that all instances are initialized to.
Brian
Hong-Gang Ch...
All-Star
74696 Points
6768 Posts
Re: MVC 2 RC + ModelMetaData.ConvertEmptyStringToNull + ModelBinder
Feb 10, 2010 02:48 AM|LINK
So far as we know that ConvertEmptyStringToNull's default value is true, check this,
http://msdn.microsoft.com/en-us/library/system.web.mvc.modelmetadata.convertemptystringtonull(VS.100).aspx
so, I think we can set 'false' to ConvertEmptyStringToNull manual.
If you have any feedback about my replies,please contactmsdnmg@microsoft.com.
Microsoft One Code Framework
bchance
Member
8 Points
19 Posts
Re: MVC 2 RC + ModelMetaData.ConvertEmptyStringToNull + ModelBinder
Feb 10, 2010 01:05 PM|LINK
I agree that you can set the value to 'false'. However the set to true is done in the MetaData constructor, and the MetaData objects are created internally by the ModelBinder for each property. This means implementing your own MetaData+MetaDataProvider to set to true or implementing your own ModelBinder to ignore the check. A simple static property to intialize would be better and clearer.
Brian
Hong-Gang Ch...
All-Star
74696 Points
6768 Posts
Re: MVC 2 RC + ModelMetaData.ConvertEmptyStringToNull + ModelBinder
Feb 11, 2010 01:36 AM|LINK
Hi Brian,
I know what is your thinking now.
But we can not change this behaviour, the Qualifier of class of Parameter is not 'virtual',
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.parameter.aspx
If you have any feedback about my replies,please contactmsdnmg@microsoft.com.
Microsoft One Code Framework
bchance
Member
8 Points
19 Posts
Re: MVC 2 RC + ModelMetaData.ConvertEmptyStringToNull + ModelBinder
Feb 11, 2010 01:51 PM|LINK
I am talking about ASP.NET MVC 2 RC, not ASP.NET server form controls.
Brian
Hong-Gang Ch...
All-Star
74696 Points
6768 Posts
Re: MVC 2 RC + ModelMetaData.ConvertEmptyStringToNull + ModelBinder
Feb 15, 2010 05:52 AM|LINK
Yes, I know that.
I mean that we can not override the constructor for the 'Parameter' class, so it mean that we can not make
ConvertEmptyStringToNull = false by default.
If you have any feedback about my replies,please contactmsdnmg@microsoft.com.
Microsoft One Code Framework
bchance
Member
8 Points
19 Posts
Re: MVC 2 RC + ModelMetaData.ConvertEmptyStringToNull + ModelBinder
Feb 15, 2010 01:46 PM|LINK
I know it is set to true in the constructor. I am suggesting the constructor be changed to pull the value from a static variable or an attribute.
If you are not a member of the ASP.NET MVC 2 team that can change the source code, then please do not reply on this thread.
Brian
bradwils
Contributor
5779 Points
691 Posts
Microsoft
Re: MVC 2 RC + ModelMetaData.ConvertEmptyStringToNull + ModelBinder
Feb 15, 2010 04:35 PM|LINK
We will not be changing the default value of this, because it lines up with the expectations of not only previous frameworks (WebForms treating empty strings as null by default), but also other platforms (f.e., RIA Services).
Use the [DisplayFormat] attribute to override this on a case-by-case basis.