I built a simple API to get some data I use in JavaScript code on a different page. I'm a bit new to MVC framework, (an inspired convert from ASP.NET forms), so not sure exactly how do I adjust the columns that it spits out. This is my code:
This code then spits out a nice JSON which I can directly use (based on UserDefaultsModel row with a certain Id). The problem is that it has a few fields that are not necessary (like Id, among some others). At this stage I manually use JavaScript to delete
those, but it would be nice if I could do it first server-side so don't have to do this client side?
Member
3 Points
16 Posts
remove columns/adjust model in API MVC
Apr 29, 2020 02:19 PM|alexi|LINK
I built a simple API to get some data I use in JavaScript code on a different page. I'm a bit new to MVC framework, (an inspired convert from ASP.NET forms), so not sure exactly how do I adjust the columns that it spits out. This is my code:
This code then spits out a nice JSON which I can directly use (based on UserDefaultsModel row with a certain Id). The problem is that it has a few fields that are not necessary (like Id, among some others). At this stage I manually use JavaScript to delete those, but it would be nice if I could do it first server-side so don't have to do this client side?
Much appreciated
All-Star
58474 Points
15793 Posts
Re: remove columns/adjust model in API MVC
Apr 29, 2020 03:05 PM|bruce (sqlwork.com)|LINK
In the model you can use the [JsonIgnore] attribute to suppress fields, but it is better to create a response model that only has what you want.
Contributor
4973 Points
4264 Posts
Re: remove columns/adjust model in API MVC
Apr 29, 2020 03:27 PM|DA924|LINK
You can use a DTO.
https://en.wikipedia.org/wiki/Data_transfer_object
https://docs.microsoft.com/en-us/aspnet/web-api/overview/data/using-web-api-with-entity-framework/part-5