Asp.net main custom binder type for just one property

I have a simple model for my main asp.net controller:

[HttpPost]
public async Task<DefaultResponse> AddCourse([FromBody]CourseDto dto)
{
     var response = await _courseService.AddCourse(dto);
     return response;
}

      

My model:

 public class CourseDto
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Genre { get; set; }
    public string Duration { get; set; }
    public string Level { get; set; }
    public string AgeRange { get; set; }
    public string Notes { get; set; }
    public bool Active { get; set; }
    public string OrganisationCode { get; set; }
}

      

I am trying to set the "OrganizationCode" value using an ad hoc action binding or filter but with no success. I would be incredible if you can advise what is the correct way to update the ethe model before executing the action.

Thank.

+7


source to share


2 answers


I'll show you a very simple custom model binder I just wrote (and tested in .Net Core 2.0):

My binding model:

public class CustomModelBinder : IModelBinder
{
    public Task BindModelAsync(ModelBindingContext bindingContext)
    {
        var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
        var value = valueProviderResult.FirstValue; // get the value as string

        var model = value.Split(",");
        bindingContext.Result = ModelBindingResult.Success(model);

        return Task.CompletedTask;
    }
}

      

My model (and note, only one property has my custom model binding annotation ):



public class CreatePostViewModel
{
    [Display(Name = nameof(ContentText))]
    [MinLength(10, ErrorMessage = ValidationErrors.MinLength)]
    public string ContentText { get; set; }

    [BindProperty(BinderType = typeof(CustomModelBinder))]
    public IEnumerable<string> Categories { get; set; } // <<<<<< THIS IS WHAT YOU ARE INTERESTER IN

    #region View Data
    public string PageTitle { get; set; }
    public string TitlePlaceHolder { get; set; }
    #endregion
}

      

What it does: It takes some text like "aaa, bbb, ccc", converts it to an array and returns it to the ViewModel.

Hope this helps.

DISCLAIMER: I am not an expert at writing binding models, I found out about this 15 minutes ago and I found your question (no helpful answer) so I tried to help. This is a very basic binding model, some improvements will definitely be needed. I learned how to write on the official documentation page .

+8


source


The [FromBody] attribute that you use in the action parameter. means you are directing the default behavior to bind to the model instead of using formatters. This is why your custom Model Binder is not working.

And [FromBody] reads the content (request body). This way you won't get the request body from your action filter, as the request body is a non-rewind stream, so it only needs to be read once (I assume you are trying to read the request body from the Action Filter).



My suggestion is to use your custom binder and remove the FromBody attribute.

+3


source







All Articles