1

Closed

ModelMetadataItem - invalid default values

description

There some discrepancies between ModelMetadataItem (MvcExtensions) and ModelMetadata (built-in mvc class from System.Web.Mvc) default property values. That makes different behavior for models with MvcExtensions and DataAnnotations attributes.
Here is a list of fields for ModelMetadataItem need to be set:
public ModelMetadataItem()
{
        ShowForDisplay = true;
        ShowForEdit = true;
        ConvertEmptyStringToNull = true;
        Order = 10000;
        RequestValidationEnabled = true;

        Validations = new List<IModelValidationMetadata>();
        AdditionalSettings = new List<IModelMetadataAdditionalSetting>();
}

If you take a look at ModelMetadata, you'll see these values:

private bool _convertEmptyStringToNull = true;
private int _order = 10000;
private bool _requestValidationEnabled = true;
private bool _showForDisplay = true;
private bool _showForEdit = true;

....


Example: to reproduce the bug you create simple model
class MyModel
{
public string Field1 {get;set;}
public string Field2 {get;set;}
}

public class MyModelModelConfiguration : ModelMetadataConfiguration<MyModel>
{
Configure(model => model.Field1).DisplayName("SomeName");
// no any settings for Field2
}

If user does not entered both fields, we'll get a model with Field1 == "" and Field2 == null. (that's bacause of ConvertEmptyStringToNull is false by default in MvcExtensions)
Closed Dec 18, 2011 at 6:22 PM by hazzik

comments

hazzik wrote Dec 18, 2011 at 6:22 PM

fixed in 2.5

ps: we have moved to github