あなたの問題は、型が別のアセンブリにしたり、動的にActivator.Create
でそれを作成しているということであるという事実とは関係ありません。次のコードは、はるかに簡単な方法で問題を示す:
[HttpPost, ValidateAntiForgeryToken]
public ActionResult NewWidget(FormCollection collection)
{
// notice the type of the latestPosts variable -> object
object latestPosts = new MyApp.Models.LatestPosts();
TryUpdateModel(latestPosts);
// latestPosts.NumPosts = 0 at this stage no matter whether you had a parameter
// called NumPosts in your request with a different value or not
...
}
問題が理由で閉じController.TryUpdateModel<TModel>
がthis connect issueで説明したようにモデルタイプを決定するためにtypeof(TModel)
代わりにmodel.GetType()
を使用するという事実(由来:by design
)。
回避策は、あなたが期待するように動作しますカスタムTryUpdateModel
方法ロールです:その後、
protected internal bool MyTryUpdateModel<TModel>(TModel model, string prefix, string[] includeProperties, string[] excludeProperties, IValueProvider valueProvider) where TModel : class
{
if (model == null)
{
throw new ArgumentNullException("model");
}
if (valueProvider == null)
{
throw new ArgumentNullException("valueProvider");
}
Predicate<string> propertyFilter = propertyName => new BindAttribute().IsPropertyAllowed(propertyName);
IModelBinder binder = Binders.GetBinder(typeof(TModel));
ModelBindingContext bindingContext = new ModelBindingContext()
{
// in the original method you have:
// ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => model, typeof(TModel)),
ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => model, model.GetType()),
ModelName = prefix,
ModelState = ModelState,
PropertyFilter = propertyFilter,
ValueProvider = valueProvider
};
binder.BindModel(ControllerContext, bindingContext);
return ModelState.IsValid;
}
をして:
[HttpPost, ValidateAntiForgeryToken]
public ActionResult NewWidget(FormCollection collection)
{
object latestPosts = new MyApp.Models.LatestPosts();
MyTryUpdateModel(latestPosts, null, null, null, ValueProvider);
// latestPosts.NumPosts will be correctly bound now
...
}
おかげで御馳走を働きました! – nfplee
ああ、ありがとう! – Rookian
このようないくつかのソリューションに直面するとき、私は本当の良いプログラマであることから遠く離れていることを認識しています。この問題はある日失われました。どうもありがとうございました。 – Alexandre