asp.net-mvc – 使用一组复杂数据类型调用UpdateModel会重置所有非绑定值吗?
|
我不确定这是否是DefaultModelBinder类中的错误或者是什么.
[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<>中,这使得其余属性设置为其默认初始值(例如Id = 0) 解决方法更新:我介绍了mvc源代码(特别是DefaultModelBinder类),这是我发现的: 该类确定我们正在尝试绑定一个集合,因此它调用方法:UpdateCollection(…),它创建一个具有null Model属性的内部ModelBindingContext.然后,将该上下文发送到方法BindComplexModel(…),该方法检查Model属性是否为null,如果是这种情况,则创建模型类型的新实例. 这就是导致重置值的原因. 因此,只填充通过表单/查询字符串/路由数据的值,其余值保持其初始化状态. 我能够对UpdateCollection(…)进行很少的更改来解决这个问题. 以下是我的更改方法: 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;
} (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- 如何将ASP.NET网站编译成单个DLL文件?
- asp.net – 如何在aspx文件中创建区域?
- ASP.NET Core 2.0和Angular 4.3文件上传进度
- asp.net-mvc – 如何更改MVC Core ValidationSummary的默认
- ASP.NET 5针对dnx451 / dnx46性能
- 加载ASP.Net MVC JSONResult jQuery DataTables
- 在ASP.NET中,HTML指令符号%#或%= etc?在服务器端执行代码
- asp.net-mvc – Razor中的Direct Model.Property与Html Hel
- asp.net – GridView的RowDataBound函数
- asp.net-mvc – ASP.NET MVC – Partial View可以有一个控制
- asp.net-mvc-2 – 如何在ASP.NET MVC中实现自定义
- asp.net-mvc – 如何从Action属性返回一个Json对
- asp.net-mvc-2 – 使用Html.LabelFor显示人类可读
- 缓存ASP.NET中的所有用户
- asp.net – ASPXGridView ClientSideEvents如何获
- asp.net – 无法加载类型’site._Default[已关闭
- asp.net核心 – 如何在asp.net core mvc项目中解
- Asp.net服务器端代码块说明
- asp.net-mvc – MVC 4 Ajax.beginform提交 – 导
- asp.net-mvc-3 – Structuremap,AutoFac或Ninjec
