我想寫自定義模型綁定器,但它給錯誤可以任何人告訴我,我在哪裏做錯了?自定義模型與IModelBinder的綁定
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcApplication1.Models
{
public class CustomModelBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
Ownership own = new Ownership();
own.name = controllerContext.HttpContext.Request.Form["fName"];
own.email = controllerContext.HttpContext.Request.Form["fEmail"];
own.PhoneNo = controllerContext.HttpContext.Request.Form["fPhoneNo"];
own.country = controllerContext.HttpContext.Request.Form["Country"];
own.address = controllerContext.HttpContext.Request.Form["Adres"];
own.office = controllerContext.HttpContext.Request.Form["Off"];
own.officeEmail = controllerContext.HttpContext.Request.Form["OffEmail"];
own.officeNo = controllerContext.HttpContext.Request.Form["OffNo"];
own.OwnershipType = controllerContext.HttpContext.Request.Form["OwnershipType"];
own.ProductId = controllerContext.HttpContext.Request.Form["ProductId"];
return own;
}
}
}
錯誤
「 'CustomModelBinder' 不實現接口構件 「System.Web.Mvc.IModelBinder.BindModel(System.Web.Mvc.ControllerContext, System.Web.Mvc。 ModelBindingContext)」
你是否在'ModelBinders.Binders.Add(typeof(Ownership),new CustomModelBinder());'in global.asax?您還需要在控制器中使用'public void MyAction([ModelBinder(typeof(CustomModelBinder))Ownership owner)' – artm 2014-10-10 12:05:52
您可以提供包含命名空間的整個文件嗎? – danyloid 2014-10-10 12:11:31
是的,我將它添加到global.asax文件中,並且在您寫入控制器時也做了同樣的事情。 感謝您的意見@artm – 2014-10-10 12:13:42