首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用IModelBinder自定义模型绑定

用IModelBinder自定义模型绑定
EN

Stack Overflow用户
提问于 2014-10-10 11:39:59
回答 1查看 5.5K关注 0票数 2

我试着写自定义模型活页夹,但它是一个错误,谁能告诉我在哪里做错了吗?

代码语言:javascript
复制
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‘不实现接口成员CustomModelBinder System.Web.Mvc.ModelBindingContext)’

EN

回答 1

Stack Overflow用户

发布于 2014-10-10 13:25:28

您正在使用的System.Web.ModelBinding名称空间中的IModelBinder。此接口的BindModel方法返回bool类型的值。

代码语言:javascript
复制
bool BindModel(
    ModelBindingExecutionContext modelBindingExecutionContext,
    ModelBindingContext bindingContext
)

如果要使用返回对象的BindModel方法,则需要实现来自BindModel命名空间的接口。

代码语言:javascript
复制
Object BindModel(
    ControllerContext controllerContext,
    ModelBindingContext bindingContext
)

您可以通过在实现此IModelBinder接口时提供完整的命名空间来检查它。喜欢

代码语言:javascript
复制
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;
}
}
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26298566

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档