Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why BindNever attribute doesn't work

I do not want do bind the Id property on my CustomerViewModel so I added a [BindNever] attribute but it is not working. What could be the solution?

I have the following:

CustomerController.cs

// PUT api/customers/5
[HttpPut("{id}")]
public async Task<IActionResult> Put([FromUri] int id, [FromBody]CustomerViewModel customer)
{
  //Implementation
}

CustomerViewModel

public class CustomerViewModel
{
    [BindNever]
    public int Id { get; set; }
    public string LastName { get; set; }
    public string FirstName { get; set; }
    public string Email { get; set; }
}

If I input the following json . The id property still gets binded

{
  "id": 100,
  "lastName": "Bruce",
  "firstName": "Wayne",
  "email": "[email protected]"
}
like image 742
bman Avatar asked Oct 07 '16 16:10

bman


People also ask

What does “BindNever” attribute do?

For example, “BindNever” attribute is used to tell Model binding, which does not bind any value to this parameter. This attribute tells to the Model binder to not to bind the value with the parameter. This attribute tells the Model binder that binding is required. If binding is not found, this attribute adds the error to the model state.

What happens when binding fails in MVC?

MVC doesn’t throw any error, when binding fails. There are many attributes contained by MVC, which help us control the behavior of the Model binding. For example, “BindNever” attribute is used to tell Model binding, which does not bind any value to this parameter. This attribute tells to the Model binder to not to bind the value with the parameter.

What does the bind parameter attribute do in the model state?

This attribute tells to the Model binder to not to bind the value with the parameter. This attribute tells the Model binder that binding is required. If binding is not found, this attribute adds the error to the model state.

What are the ASP NET Core model binding attributes?

Asp.Net Core Model Binding has a set of attributes that gives us the ability to control from what source we want to receive the binding data. In this post I’m going to go through these attributes and show how and when you can use them.


1 Answers

I add a note.

Now it's officially explained by Microsoft.

https://docs.microsoft.com/ja-jp/aspnet/core/mvc/models/model-binding?view=aspnetcore-6.0#attributes-for-complex-type-targets

https://docs.microsoft.com/ja-jp/aspnet/core/mvc/models/model-binding?view=aspnetcore-6.0#input-formatters

https://docs.microsoft.com/ja-jp/aspnet/core/mvc/models/model-binding?view=aspnetcore-6.0#frombody-attribute

In summary,

If we use the “FromBody attribute (including defaults such as HttpPost attribute)”, it depends on the input formatter and the BindNever attribute etc. will not work.

Instead, we can do so by specifying the attribute that corresponds to the input formatter. For example, for the default json It can be ignored using "System.Text.Json.Serialization.JsonIgnoreAttribute".

like image 69
takemori_kondo Avatar answered Oct 06 '22 01:10

takemori_kondo