Plaga
Plaga

Reputation: 83

mvc6 custom model binder not firing

I'm trying to figure out model binding in current mvc6 (visual studio 2015 release candidate). This is what my code looks like so far:

public class MyObjectModelBinder : IModelBinder
{
    public Task<ModelBindingResult> BindModelAsync(ModelBindingContext bindingContext)
    {

        if (bindingContext.ModelType == typeof(MyObject))
        {
            var model = new MyObject();
            return Task.FromResult(new ModelBindingResult(model, bindingContext.ModelName, true));
        }
        return Task.FromResult<ModelBindingResult>(null);

    }
}

The registration in startup.cs

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc().Configure<MvcOptions>(options =>
    {
        options.ModelBinders.Add(typeof( MyObjectModelBinder));
    });
}

My Controller:

[HttpPost]
public void ReceiveMyObject([FromBody] MyObject x)
{

}

I don't care yet about actually creating the object from the input, what troubles me is that when I debug, I see the controller firing (with x being null), but the binder function is not being called. Any ideas of what's wrong here?

[EDIT: This has already been updated]Also note, I have already seen What is the correct way to create custom model binders in MVC6? but the answer in that post is either wrong or outdated, since the example provided doesn't implement the current IModelBinder.

Thanks

EDIT: This is the javascript code used to fire the controller:

function sendMessage(i) {
    $.ajax({
        type: 'POST',
        url: 'myurl',
        data: data,
        contentType: 'application/x-www-form-urlencoded',
        dataType: 'json',
        success: function (data) { console.log(data) }
    });
}

Upvotes: 3

Views: 4827

Answers (3)

Yves
Yves

Reputation: 3902

Try to add it as the first model binder into the list. When an IModelBinder successfully sets a model, other IModelBinder won't be fired.

So if you want to give a chance to your own IModelBinder you have to make it being fired before others do the job.

This works for me:

services.Configure<MvcOptions>(options =>
{
    options.ModelBinders.Insert(0, new MyObjectModelBinder());
});

But index 0 is not the best place to put it. Just determine which ModelBinder sets the model and prevents your ModelBinder to be fired and put your ModelBinder before it.

Upvotes: 1

Horosho
Horosho

Reputation: 655

Add

public void ReceiveMyObject([ModelBinder(BinderType = typeof(MyObjectModelBinder))] MyObject x)

to method or u can set default binder for type

Upvotes: 2

Kiran
Kiran

Reputation: 58009

Remove the [FromBody] decoration on the action's parameter.

FromBody is indicated that an InputFormatter is to be used to populate this parameter. If you remove it model binders run.

Upvotes: 1

Related Questions