views:

793

answers:

2

http://www.asp.net/learn/mvc/tutorial-39-cs.aspx

We are using the above guide to implement some validation in our ASP.NET MVC app.

We receive the following error This property setter is obsolete, because its value is derived from ModelMetadata.Model now. which does not have a line number, it simply explodes when pressing the submit button to create a new message.

We are having to use the MetaData example (See bottom of the guide above) because the objects are generated in the DBML

Any suggestions about what's causing the error?

+11  A: 

You will get this error when you create a new ModelBindingContext and then attempt to set the ModelType property, in MVC 2 preview 2 or higher. For example, in a unit test for a custom model binder in older versions of MVC, I had code like the following:

    internal static T Bind<T>(string prefix, FormCollection collection, ModelStateDictionary modelState) where T:class
    {
        var mbc = new ModelBindingContext()
        {
            ModelName = prefix,
            ModelState = modelState,
            ModelType = typeof(T),
            ValueProvider = collection.ToValueProvider()
        };
        IModelBinder binder = new MyModelBinder();
        var cc = new ControllerContext();
        return binder.BindModel(cc, mbc) as T;
    }

When I updated to MVC 2 preview 2, I got the same error as you described. The fix was to change this code to this:

    internal static T Bind<T>(string prefix, FormCollection collection, ModelStateDictionary modelState) where T:class
    {
        var mbc = new ModelBindingContext()
        {
            ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, typeof(T)),
            ModelName = prefix,
            ModelState = modelState,
            ValueProvider = collection.ToValueProvider()
        };
        IModelBinder binder = new MyModelBinder();
        var cc = new ControllerContext();
        return binder.BindModel(cc, mbc) as T;
    }

Note that I have removed the assignment of ModelType, and replaced it with an assignment to ModelMetadata. Visual Studio should tell you which line of code is actually throwing this error.

Craig Stuntz
I'm going to try this out now, thanks - I would have been here for a while without you. However is this a bug? Is it somthing we are doing wrong?
Pino
Just to add, I found the error in the Dll (Line 47 DataAnnotationsModelBinder.cs) ' var innerContext = new ModelBindingContext() { Model = propertyDescriptor.GetValue(bindingContext.Model), ModelName = fullPropertyKey, ModelState = bindingContext.ModelState, ModelType = propertyDescriptor.PropertyType, ValueProvider = bindingContext.ValueProvider };'
Pino
Your code seems to use completley differant objects?
Pino
It's not a bug; it's a feature change. The code needs to be altered to support Preveiw 2. Remove the assignment to ModelType, and add an assignment to ModelMetadata. Use GetMetadataForType(model, typeof(model.GetType())), where model is what you assign to the Model property.
Craig Stuntz
Is there a newer way of performing validation? It seems a bit backward that I have to take this DLL and regrenerate it then include it into the MVC app?
Pino
Yes, preview 2 has something like the data annotation binder built in. See: http://blogs.msdn.com/pietrobr/archive/2009/10/03/asp-net-mvc-2-preview-2-client-validation.aspx
Craig Stuntz
O... Right, well i need to look at that. The link you sent is purely client side validation, the nature of the porject meens that is out of the question. Can we do server side validation? Any links or ideas?
Pino
NVM I have sussed it, the example works fine when i remove the dll. It must have been added into version 2.0 -
Pino
All validation in MVC 2 preview 2 is done on the server, always. Optionally, it can be done on the browser, too.
Craig Stuntz
This was super helpful!
Ryan Riley
+1  A: 

It would be great if you would update the "Data Annotations Model Binder Sample" in Codeplex to kill off these errors.

John