GON*_*ale 6 asp.net asp.net-mvc updatemodel
有人有运气吗?
如果我理解正确,请告诉我,如果我有一个简单的模型,请说:
public string Name { get; set; }
public string Details { get; set; }
public DateTime? Created { get; set; }
Run Code Online (Sandbox Code Playgroud)
然后我执行:
var myModel = getCurrentModelFromDb(id);
UpdateModel(myModel, "ModelName", new string { "Name", "Details" });
Run Code Online (Sandbox Code Playgroud)
这应该只更新名称和详细信息属性吗?因为假设在'created'中已经有来自db的日期,当我执行上述操作时,它似乎将我创建的日期设置为原始的01-01-0001.
此外,当我尝试使用以下方法明确排除此字段时:
UpdateModel(myModel, "ModelName",
new string { "Name", "Details" }, new string { "Created" });
Run Code Online (Sandbox Code Playgroud)
它仍被设置为01-01-0001.这是一个错误,还是一个我做错的奇怪事情?
我真正想要做的是,更新我的模型属性,其中有相应的表单字段,但保留其余的单独的数据,这些属性是从db fetch单独设置的,而不是将它们设置为null或default,这就是它当前的内容似乎在做.
我会说,也许上面和我的真实场景之间的唯一区别是我在列表上使用updateModel,所以我实际上得到了listFromDb(parentId),然后在那个选择上应用updateModel(myList,"ListPrefix")通过[0],[1]等来计算每个项目...它的工作原理,因为所有名称都在更新,但其他一切都没有.
更新:我刚刚意识到'includeProperties'可能是要定义您希望从表单中包含哪些属性,类似于绑定的工作方式.如果*是*的情况,那么我怎么能告诉它只更新某些模型属性呢?
我一直在使用 Reflector 对此进行研究...调用堆栈是:
UpdateModel()--> TryUpdateModel()--> DefaultModelBinder.BindModel()--->BindComplexModel()或BindSimpleModel()。
这是以下的反汇编BindSimpleModel():
if (bindingContext.ModelType != typeof(string))
{
if (bindingContext.ModelType.IsArray)
{
return ConvertProviderResult(bindingContext.ModelState, bindingContext.ModelName, valueProviderResult, bindingContext.ModelType);
}
Type type = ExtractGenericInterface(bindingContext.ModelType, typeof(IEnumerable<>));
if (type != null)
{
object o = this.CreateModel(controllerContext, bindingContext, bindingContext.ModelType);
Type collectionType = type.GetGenericArguments()[0];
Type destinationType = collectionType.MakeArrayType();
object newContents = ConvertProviderResult(bindingContext.ModelState, bindingContext.ModelName, valueProviderResult, destinationType);
if (typeof(ICollection<>).MakeGenericType(new Type[] { collectionType }).IsInstanceOfType(o))
{
CollectionHelpers.ReplaceCollection(collectionType, o, newContents);
}
return o;
}
}
Run Code Online (Sandbox Code Playgroud)
很明显,正在创建新元素。但我不清楚这些标志的确切逻辑,而且我现在没有时间进一步调查它。顺便说一句,BindComplexModel 的相似之处在于它似乎正在为集合类型创建新元素。
稍后我会尝试对其进行更多分析。