ModelBindingContext.ValueProvider.GetValue(key)总是返回null
我正在使用AngularJS来处理一个相当复杂的父对象,这些对象需要与服务器端的行为完全不同。基于this answer,这看起来非常稳固,我在下面创建了测试用例。我遇到的问题是,只要我输入CreateModel
函数,任何对bindingContext.ValueProvider.GetValue(key)
的调用都将返回null。我已经在调试器中检查了所有的值。对象类型似乎已加载,但尚未绑定任何值。ModelBindingContext.ValueProvider.GetValue(key)总是返回null
我的模型:
public class Menagerie
{
public Menagerie()
{
Critters = new List<Creature>();
}
public string MakeNoise()
{
return String.Join(" ", Critters.Select(c => c.MakeNoise()));
}
public List<Creature> Critters { get; set; }
}
public class Tiger : Creature
{
public Tiger() { }
public override CreatureType Type => CreatureType.Tiger;
public override string Sound => "ROAR";
}
public class Kitty : Creature
{
public Kitty() { }
public override CreatureType Type => CreatureType.Kitty;
public override string Sound => "meow";
}
public class Creature
{
public Creature() { }
public string Name { get; set; }
public virtual CreatureType Type { get; set; }
public virtual string Sound { get; }
public string MakeNoise()
{
return $"{Name} says {Sound ?? "nothing"}.";
}
public static Type SelectFor(CreatureType type)
{
switch (type)
{
case CreatureType.Tiger:
return typeof(Tiger);
case CreatureType.Kitty:
return typeof(Kitty);
default:
throw new Exception();
}
}
}
public enum CreatureType
{
Tiger,
Kitty,
}
public class CreatureModelBinder : DefaultModelBinder
{
protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)
{
CreatureType creatureType = GetValue<CreatureType>(bindingContext, "Type");
Type model = Creature.SelectFor(creatureType);
Creature instance = (Creature)base.CreateModel(controllerContext, bindingContext, model);
bindingContext.ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => instance, model);
return instance;
}
private T GetValue<T>(ModelBindingContext bindingContext, string key)
{
ValueProviderResult valueResult = bindingContext.ValueProvider.GetValue(key); // valueResult is null
bindingContext.ModelState.SetModelValue(key, valueResult);
return (T)valueResult.ConvertTo(typeof(T)); // NullReferenceException
}
}
我的脚本:
(function() {
'use strict';
angular.module('app', []).controller('CritterController', ['$scope', '$http', function ($scope, $http) {
$http.post('/a/admin/get-menagerie', { }).success(function (data) {
$scope.menagerie = data.menagerie;
});
$scope.makeNoise = function() {
$http.post('/a/admin/make-noise', { menagerie: $scope.menagerie }).success(function (data) {
$scope.message = data.message;
});
}
}]);
})();
事情我已经我已经试过只使用一个字符串来表示类名试图
,如在this answer和this one。但是,bindingContext.ValueProvider.GetValue(key)
的调用仍然返回空值,导致NullReferenceException
。
我也检查过,以确保模型正确绑定。当我将CreatureModelBinder
更改为以下内容时,一切都很准确。但是每个生物都会失去其继承的类型,并成为Creature
。
public class CreatureModelBinder : DefaultModelBinder
{
protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)
{
return base.CreateModel(controllerContext, bindingContext, modelType);
}
} // MakeNoise returns: "Shere Khan says nothing. Mr. Boots says nothing. Dr. Evil says nothing."
我希望在溶液中存在的:
当发布JSON数据,似乎所有的钥匙都在型号名称前缀。这可以简单地通过改变CreateModel
第一线需要解决:
CreatureType creatureType = GetValue<CreatureType>(bindingContext, bindingContext.ModelName + ".Type");
以前的答案:
的数据我AngularJS功能帖子由Chrome浏览器标记为“请求负载”,哪些不能(如据我所知)可以在CreateModel
访问。当我实施@ DarinDimitrov的解决方案时,数据被张贴为“表格数据”,可在CreateModel
中找到。我发现了一些有关数据类型here之间差异的更多信息,尽管AngularJS似乎无法发送除application/json之外的内容类型的数据而没有一些奇特的杂技。
我没有,但是,发现我可以在BindModel
功能访问我的对象的实例(并用不同的类型来创建一个新的实例)如下:
public class CreatureModelBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
Creature instance = (Creature)base.BindModel(controllerContext, bindingContext);
Creature newInstance = (Creature)Activator.CreateInstance(Creature.SelectFor((instance).Type));
newInstance.Name = instance.Name;
return newInstance;
}
} // MakeNoise() returns: "Shere Khan says ROAR. Mr. Boots says meow. Dr. Evil says meow."
最大的缺点我能看到对象中的每个属性都必须手动映射到新实例,这会变得很麻烦并且会产生难以跟踪的错误。
这就是我现在使用的东西,尽管如果任何人都可以提供更优雅的解决方案,我会接受这些建议。
编辑
我发现,随着应用程序/ JSON发布时,模型数据可以访问使用此: