Can';在ASP.NET MVC中,IncludeProperty似乎无法处理UpdateModel

Can';在ASP.NET MVC中,IncludeProperty似乎无法处理UpdateModel,asp.net,asp.net-mvc,updatemodel,Asp.net,Asp.net Mvc,Updatemodel,有人在这方面运气好吗 请让我知道我是否理解正确,如果我有一个简单的模型,比如: public string Name { get; set; } public string Details { get; set; } public DateTime? Created { get; set; } 然后我做了一个: var myModel = getCurrentModelFromDb(id); UpdateModel(myModel, "ModelName", new string { "Nam

有人在这方面运气好吗

请让我知道我是否理解正确,如果我有一个简单的模型,比如:

public string Name { get; set; }
public string Details { get; set; }
public DateTime? Created { get; set; }
然后我做了一个:

var myModel = getCurrentModelFromDb(id);
UpdateModel(myModel, "ModelName", new string { "Name", "Details" });
这是否应该只更新名称和详细信息属性? 因为假设“created”中已经有来自db的日期,当我执行上述操作时,它似乎将我的创建日期从原始日期设置为01-01-0001

此外,当我尝试使用以下方法显式排除此字段时:

UpdateModel(myModel, "ModelName", 
   new string { "Name", "Details" }, new string { "Created" });
它仍被设置为01-01-0001。这是一个错误,还是我做错了一件奇怪的事情

实际上,我想做的是更新模型属性,其中有相应的表单字段,但保留其余的属性,这些属性是单独从db fetch设置的,而不是将它们设置为null或default,这是它当前正在做的事情

不过我要说的是,也许上面的场景和我的真实场景之间的唯一区别是我在列表上使用updateModel,所以我实际上是从DB(parentId)获取listFromDb,然后在[0]、[1]等获取每个项的对象上应用updateModel(myList,“ListPrefix”)。。。它可以工作,因为所有的名字都在更新,但其他的一切都不是


更新:我刚刚意识到“IncludeProperty”可能是定义希望从表单中包含哪些属性,类似于绑定的工作方式。如果是这样,那么我如何告诉它只更新某些模型属性呢?

我一直在使用Reflector研究这个问题。。。调用堆栈是:

UpdateModel()
-->
TryUpdateModel()
-->
DefaultModelBinder.BindModel()
-->
BindComplexModel()
BindSimpleModel()

下面是对
BindSimpleModel()
的反汇编:

if(bindingContext.ModelType!=typeof(string))
{
if(bindingContext.ModelType.IsArray)
{
返回ConvertProviderResult(bindingContext.ModelState、bindingContext.ModelName、valueProviderResult、bindingContext.ModelType);
}
Type Type=ExtractGenericInterface(bindingContext.ModelType,typeof(IEnumerable));
if(type!=null)
{
对象o=this.CreateModel(controllerContext、bindingContext、bindingContext.ModelType);
类型collectionType=Type.GetGenericArguments()[0];
类型destinationType=collectionType.MakeArrayType();
object newContents=ConvertProviderResult(bindingContext.ModelState、bindingContext.ModelName、valueProviderResult、destinationType);
if(typeof(ICollection).MakeGenericType(新类型[]{collectionType}).IsInstanceOfType(o))
{
CollectionHelpers.ReplaceCollection(collectionType、o、newContents);
}
返回o;
}
}
很明显,有一些新元素正在被创建。不过,我不清楚这些标志的确切逻辑,我现在没有时间进一步调查。顺便说一句,BindComplexModel的相似之处在于它似乎正在为集合类型创建新元素


稍后我将尝试进行更多分析。

您好,我遵循源代码的相同路线来查看其中的情况(我的下拉列表有很多问题,有时不绑定,具体取决于selectlist来自何处…)。你是怎么做到的?能够在MVC框架中获得一些东西的调用堆栈会很有帮助。
 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;
        }
    }