我不确定这是否是DefaultModelBinder类中的错误或者是什么。 但UpdateModel通常不会更改模型的任何值,除了它找到匹配的值。 看看以下内容:

[AcceptVerbs(HttpVerbs.Post)]
public ViewResult Edit(List<int> Ids)
{
    // Load list of persons from the database
    List<Person> people = GetFromDatabase(Ids);
    // shouldn't this update only the Name & Age properties of each Person object
    // in the collection and leave the rest of the properties (e.g. Id, Address)
    // with their original value (whatever they were when retrieved from the db)
    UpdateModel(people, "myPersonPrefix", new string[] { "Name", "Age" });
    // ...
}

UpdateModel会创建 Person对象,并指定其名称<!>放大器;来自ValueProvider的Age属性并将它们放在参数List <!> lt; <!> gt;中,这使得其余属性设置为其默认初始值(例如Id = 0) 那么这里发生了什么?

有帮助吗?

解决方案

<强>更新 我介绍了mvc源代码(特别是DefaultModelBinder类),这是我发现的:

该类确定我们正在尝试绑定一个集合,因此它调用方法:UpdateCollection(...),它创建一个具有ModelBindingContext null属性的内部Model。然后,将该上下文发送到BindComplexModel(...)方法,该方法检查<=>属性<=>并创建模型类型的 new 实例(如果是这种情况)。

这就是导致重置值的原因。

因此,只填充通过表单/查询字符串/路由数据的值,其余值保持其初始化状态。

我能够对<=>进行很少的更改来解决这个问题。

以下是我的更改方法:

internal object UpdateCollection(ControllerContext controllerContext, ModelBindingContext bindingContext, Type elementType) {
IModelBinder elementBinder = Binders.GetBinder(elementType);

// build up a list of items from the request
List<object> modelList = new List<object>();
for (int currentIndex = 0; ; currentIndex++) {
    string subIndexKey = CreateSubIndexName(bindingContext.ModelName, currentIndex);
    if (!DictionaryHelpers.DoesAnyKeyHavePrefix(bindingContext.ValueProvider, subIndexKey)) {
        // we ran out of elements to pull
        break;
    }
    // **********************************************************
    // The DefaultModelBinder shouldn't always create a new
    // instance of elementType in the collection we are updating here.
    // If an instance already exists, then we should update it, not create a new one.
    // **********************************************************
    IList containerModel = bindingContext.Model as IList;
    object elementModel = null;
    if (containerModel != null && currentIndex < containerModel.Count)
    {
        elementModel = containerModel[currentIndex];
    }
     //*****************************************************
    ModelBindingContext innerContext = new ModelBindingContext() {
        Model = elementModel, // assign the Model property
        ModelName = subIndexKey,
        ModelState = bindingContext.ModelState,
        ModelType = elementType,
        PropertyFilter = bindingContext.PropertyFilter,
        ValueProvider = bindingContext.ValueProvider
    };
    object thisElement = elementBinder.BindModel(controllerContext, innerContext);

    // we need to merge model errors up
    VerifyValueUsability(controllerContext, bindingContext.ModelState, subIndexKey, elementType, thisElement);
    modelList.Add(thisElement);
}

// if there weren't any elements at all in the request, just return
if (modelList.Count == 0) {
    return null;
}

// replace the original collection
object collection = bindingContext.Model;
CollectionHelpers.ReplaceCollection(elementType, collection, modelList);
return collection;

}

其他提示

Rudi Breedenraed刚刚写了一篇优秀的帖子描述这个问题和一个非常有用的解决方案。他重写了DefaultModelBinder,然后当它遇到要更新的集合时,它实际上更新了项目,而不是像默认的MVC行为那样创建它。有了这个,UpdateModel()和TryUpdateModel()行为与根模型和任何集合都是一致的。

您刚刚给我一个挖掘ASP.NET MVC 2源代码的想法。 我已经在这两个星期里苦苦挣扎了。我发现您的解决方案不适用于嵌套列表。我在UpdateCollection方法中放了一个断点,它永远不会被击中。似乎模型的根级别需要成为此方法的列表

这就是我所拥有的模型。我还有一个级别的通用列表,但这只是一个快速的样本..

public class Borrowers
{
   public string FirstName{get;set;}
   public string LastName{get;set;}
   public List<Address> Addresses{get;set;}
}

我想,我需要深入挖掘以了解发生了什么。

更新: UpdateCollection仍在asp.net mvc 2中调用,但上面修复的问题与此相关这里

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top