Custom Model binder :Can't bind parameter 'xxx' because it has conflicting attributes on it
Asked Answered
S

1

5

I am using a custom model binder in order to put some custom logic in my model Binding.

Here's my DTOs:

public class TaskDto
{
    public int Id { get; set; }

    [MaxLength(100), Required]
    public string Name { get; set; }

    public List<StepDto> Steps { get; set; }

    public List<ResourceDTO> Resources { get; set; }
}

Step DTO:

public class StepDto
{
    public int Id { get; set; }

    [MaxLength(100)]
    public string Description { get; set; }

    public StepType StepType { get; set; }
}

ResourceDTO:

public class ResourceDTO
{
    public int Id { get; set; }

    [MaxLength(250), Required]
    public string Title { get; set; }

    [Required]
    public string Link { get; set; }

    public string MetaTagUrl { get; set; }

    [Required, Range(1, 1)]
    public ResourceType ResourceType { get; set; }
}

Where ResourceType is an enumeration (only has 1 as value for now.)

I tried creating a custom model binder using this link.

Here's my API Action method signature:

    [HttpPut]
    [Route("datacapture/{activityId}/tasks")]
    [Authorize]
    public async Task<IHttpActionResult> UpdateAllDataCaptureActivities(int activityId, [FromBody][ModelBinder] TaskDto tasks)
     {
      ...
     }

I am getting the following error on calling the API:

"Message": "An error has occurred.",
  "ExceptionMessage": "Can't bind parameter 'tasks' because it has conflicting attributes on it.",
  "ExceptionType": "System.InvalidOperationException",
Sanbo answered 17/5, 2017 at 12:44 Comment(0)
O
14

Don't use [FromBody] and [ModelBinder] at the same time.

Otiliaotina answered 11/12, 2017 at 22:17 Comment(1)
This should be flagged as the answerKirbie

© 2022 - 2024 — McMap. All rights reserved.