Skip to content
This repository has been archived by the owner on May 11, 2023. It is now read-only.

Commit

Permalink
dotnet 6 updates added to text and links
Browse files Browse the repository at this point in the history
  • Loading branch information
speudusa committed Oct 20, 2022
1 parent 4e457ec commit dbbe5ad
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
Validation Attributes
=====================

Within the ViewModel or model of a C# web application, we can define validation rules using attributes.
Within the ViewModel or model of a C# web application,
we can define validation rules using attributes.
Validation attributes can be applied to model fields.

.. index::
Expand All @@ -12,7 +13,8 @@ Validation attributes can be applied to model fields.
Common Attributes
------------------

We'll use only a few of these attributes, but you can find a full list in the `documentation <https://docs.microsoft.com/en-us/aspnet/core/mvc/models/validation?view=aspnetcore-3.1#built-in-attributes>`_.
We'll use only a few of these attributes, but you can find a
full list in the `documentation <https://learn.microsoft.com/en-us/aspnet/core/mvc/models/validation?view=aspnetcore-6.0#built-in-attributes>`_.

.. list-table:: Common Validation Attributes
:header-rows: 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ In the ``Add`` View, you will need to add more ``class`` attributes for the libr
<span asp-validation-for="Name" class="text-danger"></span>
</div>

Note in line 10 the addition of ``class="form-control`` and in line 11 ``class="text-danger"``.
Note in line 10 the addition of ``class="form-control"`` and in line 11 ``class="text-danger"``.
These attributes will be noted by the jQuery Unobtrusive Validation library and print our red
if the client does not meet the requirements.

Expand Down
35 changes: 23 additions & 12 deletions src/chapters/enums/enums-in-models.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
Enums in Model Classes
======================

One application of enum types is to represent categories of objects. We will take this approach in our ``CodingEvents`` application to categorize events based on their type, such as *conference* or *meetup*.
One application of enum types is to represent categories of objects.
We will take this approach in our ``CodingEvents`` application to
categorize events based on their type, such as *conference* or *meetup*.

Enum Types in Models - Video
----------------------------
Expand All @@ -11,8 +13,11 @@ Enum Types in Models - Video

.. admonition:: Note

If you ever want to verify what code the video starts with, check out the `display-error-messages <https://github.com/LaunchCodeEducation/CodingEventsDemo/tree/display-error-messages>`__ branch in ``CodingEventsDemo``.
If you ever want to verify what code the video ends with, check out the `enums <https://github.com/LaunchCodeEducation/CodingEventsDemo/tree/enums>`__ branch in ``CodingEventsDemo``.
If you ever want to verify what code the video starts with,
check out the `display-error-messages <https://github.com/LaunchCodeEducation/CodingEventsDemo/tree/display-error-messages>`__ \
branch in ``CodingEventsDemo``. If you ever want to verify what code the video ends with,
check out the `enums <https://github.com/LaunchCodeEducation/CodingEventsDemo/tree/enums>`__
branch in ``CodingEventsDemo``.

Enum Types in Models - Text
---------------------------
Expand Down Expand Up @@ -53,7 +58,8 @@ we create a ``Type`` property in ``Event`` amongst the other properties declared

// Event methods

Once we have added an ``EventType`` property to our model, we need to add an ``EventType`` property to our ViewModel.
Once we have added an ``EventType`` property to our model, we need to add an
``EventType`` property to our ViewModel.

Add an Enum Property to a ViewModel
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -67,7 +73,8 @@ The first thing we need to do is add an ``EventType`` property to ``AddEventView

public EventType Type { get; set; }

Now that we have a property to hold the event type that the user selects, we need to create a list of all of the possible event types.
Now that we have a property to hold the event type that the user selects,
we need to create a list of all of the possible event types.
We will use this list to populate the ``select`` element in a later step.

.. sourcecode:: csharp
Expand All @@ -82,13 +89,17 @@ We will use this list to populate the ``select`` element in a later step.
};

Because the ``EventType`` options are not changing, we can populate this property with a default value.
We add each of the constants to the ``EventTypes`` list. ``SelectListItem`` is an ASP.NET-provided class that represents each ``<option>``
element in a ``<select>`` element. The ``SelectListItem`` constructor requires a ``Text`` and ``Value`` property assignment.
The ``Text`` property sets the displayed text in the ``<option>`` tag. This is created by getting each ``EventType`` and casting it to a ``string`` type.
The ``Value`` property sets the ``value`` attribute on the ``<option>`` tag. This is created by getting each ``EventType``, casting it to
its implicit ``int`` value, and then casting that to a ``string`` type.

This list only exists in ``AddEventViewModel`` because we need it only for the purposes of displaying all of the options.
We add each of the constants to the ``EventTypes`` list. ``SelectListItem``
is an ASP.NET-provided class that represents each ``<option>`` element in a ``<select>`` element.
The ``SelectListItem`` constructor requires a ``Text`` and ``Value`` property assignment.
The ``Text`` property sets the displayed text in the ``<option>`` tag.
This is created by getting each ``EventType`` and casting it to a ``string`` type.
The ``Value`` property sets the ``value`` attribute on the ``<option>`` tag.
This is created by getting each ``EventType``, casting it to its implicit ``int`` value,
and then casting that to a ``string`` type.

This list only exists in ``AddEventViewModel`` because we need it only for
the purposes of displaying all of the options.
We do not need a list of the different event types in our ``Event`` model.
We just need the type of one event.
This is another great reason to use a ViewModel!
Expand Down

0 comments on commit dbbe5ad

Please sign in to comment.