I created a custom binder that looks like this:
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var o = new MetaObject();
o.OnBinderLoad();
o.StatusTypeId = Convert.ToInt32(bindingContext.ValueProvider.GetValue("StatusTypeId").AttemptedValue);
o.Comments.setValue(bindingContext.ValueProvider.GetValue("Comments").AttemptedValue);
o.EffectiveDate.setValue(bindingContext.ValueProvider.GetValue("EffectiveDate").AttemptedValue);
// bindingContext.ModelMetadata =
ModelBindingContext newBindingContext = new ModelBindingContext()
{
ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => o, typeof(MetaObject)),
ModelState = bindingContext.ModelState,
ValueProvider = bindingContext.ValueProvider
};
var returnValue = base.BindModel(controllerContext, newBindingContext);
return returnValue;
}
I run the debugger and the value of o and newBindingContext.ModelMetadata has the correct data before the call to base.BindModel. After that point Comments and EffectivDate are null.
How do I trace this and why would that happen.
Thanks for the help
Because I was inheriting from DefaultBinder, the base.bindmodel was overriding what I did. I followed the example below and everything is binding correctly.
http://mgolchin.net/posts/20/dive-deep-into-mvc-imodelbinder-part-1