ASP.NET MVC Model Binding - Different JSON Property Names and C # Model Properties

I annotated the properties of my model classes as shown below.

[DataMember(Name = "EN")] public string EmployeeName{ get; set; } 

In general, this leads to compact JSON (I am serializing using the JSON.NET serializer).

However, when the JSON containing these smaller names is passed using the POST or PUT request for the controllers, the binding of the ASP.NET MVC model does not allow the JSON property β€œEN” to be correctly mapped to EmployeeName . It expects EmployeeName in JSON.

Any thoughts on how to fix this?

+4
source share
1 answer

You cannot do this out of the box. You have two ways to solve this issue: renaming properties in your view model (after all, this is a view model, so it should cope with your limitations), or you can try writing your own ModelBinder , which will take DataMember annotations into account when binding properties.

+4
source

All Articles