15

I'm wondering if there is a way to use the built in model binding similar to the internal model binding that occurs before a controller action.

My problem is that I want to be able to control the binding as I won't know the type of object to bind until I'm actually in the context of the controller action.

I understand I can inherit the DefaultModelBinder to perform custom binding, but I'm happy with what's already on offer, and just want to utilise it - take this ideal example to get an idea of what I'm after:

public ActionResult DoCustomBinding(string modelType)
{
    ... // logic to determine type to check and create strong 'actual' type

    object model = BindModel(actualType);

    ... // do something with bound model

    return View();
}

I've looked into using the DefaultModelProvider but unsure if this is the right way of going about this and I wasn't sure how to obtain the ModelBindingContext.

tereško
  • 57,247
  • 24
  • 95
  • 149
Phil Cooper
  • 2,972
  • 38
  • 61
  • You're right. It's poor behaviour on such a useful site, I've given myself a thorough ticking off. – Phil Cooper Apr 04 '12 at 20:46
  • I'll try and be more specific, my ultimate goal is to be able to validate a single property of a class decorated with validation attributes. So, armed with only a string name of the type to check, field name(s) and value(s) - I'd like to be able to bind the model (which I'll need to work out from the type) then perform checks on it. – Phil Cooper Apr 04 '12 at 20:50
  • I'm going to take a look around the ControllerActionInvoker http://aspnet.codeplex.com/SourceControl/changeset/view/72551#266452 it looks like it might give me an idea of how its done internally. – Phil Cooper Apr 04 '12 at 21:20

3 Answers3

12

If anyone comes across this question from google as I did here is the answer: How to gain control over model binding?

In short: TryUpdateModel is the method you are looking for.

Community
  • 1
  • 1
Kugel
  • 18,498
  • 14
  • 69
  • 103
1

If you want to validate only specific parts of a model, this might be duplicate of an question I previously answered MVC Partial Model Updates.

The cool part about using System.ComponentModel.DataAnnotations.MetadataType is that the model binder will keep binding to a derived object of which is basically the same as the base object, just with different display/validation metadata.

Community
  • 1
  • 1
Erik Philips
  • 51,408
  • 11
  • 123
  • 146
  • Thanks - this looks useful, I've just managed to get the binding working so I've got a couple of solutions I can look at now... – Phil Cooper Apr 04 '12 at 21:45
0

Have you looking into the IModelBinder interface?

public class CustomModelsBinder : IModelBinder {
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { }
}

And then add this to your global.asax file:

protected override void OnApplicationStarted() {
   ModelBinders.Binders.Add(typeof(CustomModels), new CustomModelsBinder());
}
Josh
  • 9,896
  • 12
  • 55
  • 103
  • Not quite, and I already use something similar to work with enums. I want to use the internal engine and sort of say, here's the type I want to create and bind to, go create and bind to it and bring back the result (outside the normal routine of a controller action). – Phil Cooper Apr 04 '12 at 20:52