Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Has IModelBinder signature changed in MVC 5?

I am just following a MVC 5 book and it is creating a custom model binder for its shopping cart. What he has in the book is:

“public class CartModelBinder : IModelBinder {
        private const string sessionKey = "Cart";

        public object BindModel(ControllerContext controllerContext,
            ModelBindingContext bindingContext) {”

Excerpt From: Adam Freeman. “Pro ASP.NET MVC 5.” iBooks. 

And when I started typing and created my class it automatically created this:

public class CartModelBinder: IModelBinder
    {
        private const string sessionKey = "Cart";
        public bool BindModel(ModelBindingExecutionContext modelBindingExecutionContext, ModelBindingContext bindingContext)
        {

That one returns an objects and accepts different parameters than the one IDE created for me. So now what should we do?

like image 930
Bohn Avatar asked Feb 12 '23 07:02

Bohn


1 Answers

The book is referring to System.Web.Mvc.IModelBinder; your implementation is using System.Web.ModelBinding.IModelBinder.

There are actually 3 different IModelBinder interfaces defined that you may encounter. The third definition is contained in System.Web.Http and is used for Web API.

This answer covers the why. In brief, they are different implementations for different purposes ( MVC, Web Forms, and Web API).

like image 67
Tim M. Avatar answered Feb 14 '23 00:02

Tim M.