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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With