Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC ViewModel scope
    text
    copied!<p>This may be a matter of taste but when creating ViewModels is it best practice to go with multiple public classes like below (bear in mind these 3 classes are servicing just a single view):</p> <pre><code>namespace WebApp.Areas.Commerce.Models.ViewModels { public class TravellersViewModel { public TicketHolder TicketHolder { get; set; } public List&lt;Traveller&gt; TicketMembers { get; set; } public int MaxAge { get; set; } public bool ShowAddress { get; set; } } public class TicketHolder { public string EmailAddress { get; set; } public string TelephoneNumber { get; set; } public string MobileNumber { get; set; } public string Address1 { get; set; } public string Postcode { get; set; } public string Country { get; set; } } public class Traveller { public int Id { get; set; } public string Title { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public int? Age { get; set; } public bool Policyholder { get; set; } public Traveller() { Title = "Mr"; } } } </code></pre> <p>Or is it best to nest the child classes within the scope of the single view model</p> <pre><code>namespace WebApp.Areas.Commerce.Models.ViewModels { public class TravellersViewModel { public TicketHolderDetails TicketHolder { get; set; } public List&lt;Traveller&gt; TicketMembers { get; set; } public int MaxAge { get; set; } public bool ShowAddress { get; set; } public class TicketHolderDetails { public string EmailAddress { get; set; } public string TelephoneNumber { get; set; } public string MobileNumber { get; set; } public string Address1 { get; set; } public string Postcode { get; set; } public string Country { get; set; } } public class Traveller { public int Id { get; set; } public string Title { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public int? Age { get; set; } public bool Policyholder { get; set; } public Traveller() { Title = "Mr"; } } } } </code></pre> <p>I quite like the second way of building the viewmodel as it keeps everything togther.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload