Exercises Solutions: ViewModels and Model ValidationΒΆ

Event information to add:

  1. Add a property to collect information about where the event will take place.
// In the Events class

public string Location { get; set; }

   //  In the Event constructor
   public Event( ... string location)
      {

            Location = location;

      }

This property should not be null or blank.

// In the AddEventViewModel class

[Required(ErrorMessage = "Location information is required.")]
public string Location { get; set; }

Return to exercises

3. Add columns for the location and number of attendees to the Events/Index.cshtml view. Line numbers for reference only.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
   <!-- inside your table -->
      <th>
         Location
      </th>
      <th>
         Number of Attendees
      </th>
      @foreach (var evt in Model)
      {
         <tr>
               <td>@evt.Id</td>
               <td>@evt.Name</td>
               <td>@evt.Description</td>
               <td>@evt.ContactEmail</td>
               <td>@evt.Location</td>
               <td>@evt.NumberOfAttendees</td>
         </tr>
      }
   </table>

Return to exercises