gpt4 book ai didi

c# - 使用 IModelBinder 自定义模型绑定(bind)

转载 作者:太空宇宙 更新时间:2023-11-03 19:06:33 25 4
gpt4 key购买 nike

我正在尝试编写自定义模型 Binder ,但出现错误,有人能告诉我哪里做错了吗?

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' does not implement interface member 'System.Web.Mvc.IModelBinder.BindModel(System.Web.Mvc.ControllerContext, System.Web.Mvc.ModelBindingContext)'

最佳答案

您正在使用的 IModelBinder 来自 System.Web.ModelBinding 命名空间。该接口(interface)的 BindModel 方法返回 bool 类型的值。

bool BindModel(
ModelBindingExecutionContext modelBindingExecutionContext,
ModelBindingContext bindingContext
)

如果你想使用返回对象的 BindModel 方法,那么你需要从 System.Web.Mvc 命名空间实现接口(interface)。

Object BindModel(
ControllerContext controllerContext,
ModelBindingContext bindingContext
)

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

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;
}
}

关于c# - 使用 IModelBinder 自定义模型绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26298566/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com