Thursday, May 31, 2018

Make a ViewModels folder for your ViewModels.

It should probably sit at the root level beside the Views folder in an ASP.NET Core MVC application. This is one of the things touched on on pages 76 and 77 of "ASP.NET Core 2 and Angular 5" by Valerio De Sanctis, the other is that if you do an Add > New Item... at the Controller folder and actually pick the MVC Controller Class option that you may be taken through a wizard which drags in other dependencies and that may be undesirable. Pages 76 and 77 have you make a ViewModel and the book's example took me back in time to my time at AMD as it both uses C# regions (complete with #endregion) and Newtonsoft which people used to use to serialize stuff to JSON before it was just a feature in ASP.NET MVC. The two uses of attributes at a ViewModel are:

  1. using System.ComponentModel; as a namespace allows for [DefaultValue(0)] above int type getsetters to provide a default value of zero at a JSON object should there be no setting at all.
  2. using Newtonsoft.Json; as a namespace is used, and slapped on the class itself is [JsonObject(MemberSerialization.OptOut)] while [JsonIgnore] on a getsetter creates an effect which will keep that getsetter from making a dancer partner for itself at the JSON object that will be made for the ViewModel.

 
 

Addendum 6/2/2018: I think the wizard kicks in when one right-clicks on the Controllers folder and picks Add > Controller... in lieu of Add > New Item...

No comments:

Post a Comment