2011-06-01 5 views

Répondre

3

Vous devez remplacer la méthode bindModel de la classe de base DefaultModelBinder:

public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) 
    { 
     if (bindingContext.ModelType == typeof(YourType)) 
     { 
      var instanceOfYourType = ...; 
      // load YourType from DB etc.. 

      var newBindingContext = new ModelBindingContext 
      { 
       ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => instanceOfYourType, typeof(YourType)), 
       ModelState = bindingContext.ModelState, 
       FallbackToEmptyPrefix = bindingContext.FallbackToEmptyPrefix, 
       ModelName = bindingContext.FallbackToEmptyPrefix ? string.Empty : bindingContext.ModelName, 
       ValueProvider = bindingContext.ValueProvider, 
      }; 
      if (base.OnModelUpdating(controllerContext, newBindingContext)) // start loading.. 
      { 
       // bind all properties: 
       base.BindProperty(controllerContext, bindingContext, TypeDescriptor.GetProperties(typeof(YourType)).Find("Property1", false)); 
       base.BindProperty(controllerContext, bindingContext, TypeDescriptor.GetProperties(typeof(YourType)).Find("Property2", false)); 

       // trigger the validators: 
       base.OnModelUpdated(controllerContext, newBindingContext); 
      } 

      return instanceOfYourType; 
     }    
     throw new InvalidOperationException("Supports only YourType objects"); 
    } 
+0

Ai-je besoin de lier chaque propriété manuellement après le chargement de la base de données ou que cela se produira encore automatiquement? – kroehre

+0

Vous devez lier manuellement chaque propriété ou la déléguer à la classe de base. – m0sa

Questions connexes