I ended up having to rely on all the data having been bound. This was the solution I ended up using. placing the code in OnModelUpdated allowed me to rely on the other properties having been set.
public class AuditedEntityModelBinder : DefaultModelBinder
{
protected override void OnModelUpdated(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
if (bindingContext.ModelType.IsSubclassOfGeneric(typeof(AuditedEntity<>)))
{
string name = controllerContext.HttpContext.User.Identity.Name;
if(!name.IsNullOrEmpty())
{
if((bool) bindingContext.ModelType.GetProperty("IsNew").GetValue(bindingContext.Model, null))
{
bindingContext.ModelType.GetProperty("CreatedBy").SetValue(bindingContext.Model, name, null);
bindingContext.ModelType.GetProperty("Created").SetValue(bindingContext.Model, DateTime.Now, null);
}
else
{
bindingContext.ModelType.GetProperty("ModifiedBy").SetValue(bindingContext.Model, name, null);
bindingContext.ModelType.GetProperty("Modified").SetValue(bindingContext.Model, DateTime.Now, null);
}
}
}
base.OnModelUpdated(controllerContext, bindingContext);
}
}