我有一个特定操作的传入请求(来自 Facebook 的 Credits 处理),该操作将具有不同的内容,因此我有不同的模型类来处理它。
这是我的行动:
public ActionResult Index([ModelBinder(typeof(FacebookCreditModelBinder))] IFacebookRequest facebookRequest)
{
if (facebookRequest is FacebookPaymentsGetItemsRequest)
{
// do whatever
}
}
这是我的模型活页夹。
public class FacebookCreditModelBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var binder = new DefaultModelBinder();
// how to change the model here in the bindingContext?
return binder.BindModel(controllerContext, bindingContext);
}
}
FacebookPaymentsGetItemsRequest
例如,如果传入的变量“方法”是“payments_get_items”并且FacebookPaymentsStatusUpdateRequest
如果方法是“payments_status_update”并且我不知道如何在 bindingContext 中更改模型的类型,我想创建一个对象。是否可以在自定义模型绑定器中更改模型的类型?
其他方法:我也使用 BindModel 进行了尝试,我能够返回正确的对象,但所有属性都为 null,因为默认模型绑定器没有填充它:
public override object BindModel(ControllerContext controllerContext,
ModelBindingContext bindingContext)
{
NameValueCollection form = controllerContext.HttpContext.Request.Form;
if (form.Get("method") == "payments_get_items")
{
return new FacebookPaymentsGetItemsRequest();
}
...