C# 使用IModelBinder自定义模型绑定
我试图编写自定义模型活页夹,但它给出了一个错误。有人能告诉我哪里做错了吗C# 使用IModelBinder自定义模型绑定,c#,asp.net-mvc,C#,Asp.net Mvc,我试图编写自定义模型活页夹,但它给出了一个错误。有人能告诉我哪里做错了吗 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 {
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)'
您使用的IModelBinder来自System.Web.ModelBinding命名空间。此接口的BindModel方法返回bool类型的值
bool BindModel(
ModelBindingExecutionContext modelBindingExecutionContext,
ModelBindingContext bindingContext
)
若您想使用返回对象的BindModel方法,那个么您需要实现来自System.Web.Mvc命名空间的接口
Object BindModel(
ControllerContext controllerContext,
ModelBindingContext bindingContext
)
在实现这个IModelBinder接口时,您可以通过提供完整的名称空间来检查它
public class CustomModelBinder : System.Web.Mvc.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;
}
}
您是否已将其添加到
ModelBinders.Binders.Add(typeof(Ownership),new CustomModelBinder());
中的global.asax?您还需要public void MyAction([ModelBinder(typeof(CustomModelBinder))]所有权所有者)
在控制器中,您可以提供包含名称空间的整个文件吗?是的,我在global.asax文件中添加了它,并且与您在控制器中编写的操作相同。谢谢您的评论@artm@danyloid:如何与您共享文件?Thanks@Asadkamal您需要重建应用程序。如果没有帮助,请清理bin
obj
和临时ASP.NET文件
(最后一个文件的位置取决于您使用的web服务器的类型)我尝试过,但它不起作用,在编译时会出现相同的错误