Asad kamal
Asad kamal

Reputation: 45

Custom model binding with IModelBinder

I am trying to write Custom model binder but it is giving an error can any one told me where i am doing it wrong?

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

}

Error

"'CustomModelBinder' does not implement interface member 'System.Web.Mvc.IModelBinder.BindModel(System.Web.Mvc.ControllerContext, System.Web.Mvc.ModelBindingContext)'

Upvotes: 2

Views: 5729

Answers (1)

Mukund
Mukund

Reputation: 1689

The IModelBinder which you are using is from System.Web.ModelBinding namespace. The BindModel method of this interface returns value of type bool.

bool BindModel(
    ModelBindingExecutionContext modelBindingExecutionContext,
    ModelBindingContext bindingContext
)

If you want to use BindModel method which returns object then you need to implement interface from System.Web.Mvc namespace.

Object BindModel(
    ControllerContext controllerContext,
    ModelBindingContext bindingContext
)

You can check it by providing complete namespace while implementing this IModelBinder interface. Like

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

Upvotes: 7

Related Questions