在 ASP.Net MVC 模型绑定(bind)器中,可以创建绑定(bind)类型的对象,然后更新其属性。
例如
public override object BindModel(ControllerContext controllerContext,
ModelBindingContext bindingContext)
{
ParentType boundModel = null;
if (bindingContext.ModelType == typeof(ParentType))
{
var myFactory = new MyFactory();
var someValue = bindingContext.ValueProvider.GetValue
("someFieldId").AttemptedValue;
ChildType child = myFactory.Create(someValue);
BindModel(child);
boundModel = child;
}
return boundModel;
}
在这段代码中,我想知道是否有类似于 BindModel(child) 调用的东西,有点像来自 Controller 的 TryModelUpdate()?
最佳答案
我认为解决您的问题的更好方法是派生自 DefaultModelBinder(我认为您可能是),然后覆盖 CreateModel 方法而不是 BindModel 方法。
通过从中返回您的 Child 对象,您应该沿着您正在寻找的路径前进。
关于c# - 将数据绑定(bind)到 Asp.net MVC 中的现有对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2298320/