.NET Deck 1 Flashcards
Write out the .Net class for this Contact object: (see description)
Bob Smith is 30 years old
Has two direct reports. They are:
- Sally Smith, 30 y/o
- Jane Doe 34 y/o
public class Contact { public string FirstName {get; set;} public string LastName {get; set;} public int Age {get; set;} public Contact[] Reports {get; set;} }
If this class was going to participate in Model binding in the MVC request life cycle, is it currently set up to report as invalid when no data is sent to the Server?(Assume FirstName & Last Name are required)
With first and last name being required it would check to see if those two are null or not.
What would you have to do to make this model report as “Invalid” in such case?
public class Contact { [Required] [StringLength(maximumLength: 50, MinimumLength = 6, ErrorMessage = "The property {0} should have {1} maximum characters and {2} minimum characters")] public string FirstName {get; set;} [Required] [StringLength(maximumLength: 50, MinimumLength = 6, ErrorMessage = "The property {0} should have {1} maximum characters and {2} minimum characters")] public string LastName {get; set;} public int Age {get; set;} public Contact[] Reports {get; set;} }
What namesspace does these attributes live in?
Contact
What are all the different types of validations that these attributes let you perform?
Required Range RegularExpression DataType EnumDataType Validation StringLength
Decorate this class to the following specs (see description)
First Name and Last Name should be at least 3 chars long and no more than 50
Age should be at least 18 years old
What is Model Binding (ASP.Net) and why is it important?
Model binding is a mechanism ASP.NET MVC uses to create parameter objects defined in controller action methods. The parameters can be of any type, from simple to complex ones. It simplifies working with data sent by the browser because data is automatically assigned to the specified model.
What are fundamentals that a developer should consider when working to wire up model binding correctly?
To have corresponding properties in html tags nested in tags ensure that the “name” attributes matches the property in the model
What are the differences between an Api Controller and “regular/view” Controller in .Net?
ApiControllers are specialized in returning data and provide a REST-ful API by convention.
“Regular” Controller - contains ActionResult classes that return a View
When creating a new controller, what class do you inherit from?
controllerBase