我正在使用最新的 WebAPI 2。我遇到了默认 ModelBinder 没有正确显示发布值的问题。我确实验证了该值已正确回传到服务器,我能够使用请求对象检索该值。所以决定实现我自己的自定义模型绑定器。现在我遇到了这个自定义活页夹永远不会被调用的问题,只有默认活页夹被调用。我错过了什么?
WebAPI 方法
public IHttpActionResult PutOrg([ModelBinder(typeof(MyOrgModelBinder))] Org org)
{
}
自定义模型绑定器
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Http.Controllers;
using System.Web.Http.ModelBinding;
using System.Web.Http.ValueProviders;
namespace SM4.Models
{
public class MyOrgModelBinder : IModelBinder
{
public bool BindModel(HttpActionContext controllerContext,
ModelBindingContext bindingContext)
{
var org = new Org
{
OrgCode_PK = request.Form.Get("OrgCode_PK"),
OrgHeadCode_FK = request.Form.Get("OrgHeadCode_FK"),
Code = request.Form.Get("Code"),
Name = "THIS is FROM BINDER",
Description = request.Form.Get("Description"),
Address1 = request.Form.Get("Address1"),
Address2 = request.Form.Get("Address2"),
CountryCode_FK = request.Form.Get("CountryCode_FK"),
StateCode_FK = request.Form.Get("StateCode_FK"),
CityCode_FK = request.Form.Get("CityCode_FK"),
ZIP = request.Form.Get("ZIP"),
RowStatusCode_FK = request.Form.Get("RowStatusCode_FK"),
EffectiveDate = DateTime.ParseExact(request.Form.Get("EffectiveDate"), "yyyy-MM-dd",
System.Globalization.CultureInfo.InvariantCulture),
TerminationDate = DateTime.ParseExact(request.Form.Get("TerminationDate"), "yyyy-MM-dd",
System.Globalization.CultureInfo.InvariantCulture),
};
bindingContext.Model = org;
return true;
}
}
}