我想知道是否有一种方法可以使用类似于在 Controller 操作之前发生的内部模型绑定(bind)的内置模型绑定(bind)。
我的问题是我希望能够控制绑定(bind),因为在我实际处于 Controller 操作的上下文中之前我不知道要绑定(bind)的对象类型。
我知道我可以继承 DefaultModelBinder 来执行自定义绑定(bind),但我对已经提供的功能很满意,只是想利用它 - 以这个理想示例来了解什么我在追求:
public ActionResult DoCustomBinding(string modelType)
{
... // logic to determine type to check and create strong 'actual' type
object model = BindModel(actualType);
... // do something with bound model
return View();
}
我研究过使用 DefaultModelProvider,但不确定这是否是正确的方法,而且我不确定如何获取 ModelBindingContext。
最佳答案
如果有人像我一样从谷歌上遇到这个问题,答案就是:How to gain control over model binding?
简而言之:TryUpdateModel 就是您正在寻找的方法。
关于c# - 使用 .Net Mvc 手动绑定(bind)模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10018830/