remove columns/adjust model in API MVC RRS feed

  • Question

  • User576772373 posted

    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:

        public class GetUserDefaultsAPIController : Controller
            private readonly ApplicationDbContext _context;
            public GetUserDefaultsAPIController(ApplicationDbContext context)
                _context = context;
            public async Task<ActionResult<MyMVC.Models.UserDefaults>> GetDefaultsAPI()
                int ProfileId = GetSelectedProfileIdfromCookie(); // custom function  
                var UserDefaultsModel = await _context.UserDefaults.FirstOrDefaultAsync(m => m.cId == ProfileId);
                if (UserDefaultsModel == null)
                    return NotFound();
                return UserDefaultsModel ;

    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

    Wednesday, April 29, 2020 2:19 PM


All replies