Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
Now it’s time to see how we can leverage MVC’s support for unobtrusive client-side validation to improve our overall user experience.
Follow Along
To follow along commiting your changes to this course, you'll need to fork the aspnet-fitness-frog repo. Then you can clone, commit, and push your changes to your fork like this:
git clone <your-fork>
cd aspnet-fitness-frog
git checkout tags/v4.5 -b implementing-unobtrusive-client-side-validation
Loading Scripts Using Layout Sections
There are two ways that we can load the scripts that we need to enable client-side validation: we can add the scripts to the layout page so every page in our web app has access to them or we can add the scripts just to the pages that need to have access to them.
Adding the scripts to the layout page makes it easier to support client-side validation throughout your web application or app. But for some situations, it might make sense to not require those additional files for pages that will never need client-side validation.
In this course, we chose to add the scripts to our layout page—for ease of use. Let’s take a look at how to add the scripts to just the views that need them.
First, we need to add a new “render section” to our layout page.
@RenderSection("scripts", required: false)
I typically put this “scripts” section just after any other script includes that are on the layout page. Now that we have a “scripts” section on our layout page, we can add the following code to any of our app’s views.
@section scripts {
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
}
That’s all there is to it! Remember, if you use this approach, you’ll need to add that @section
directive to each view that will need support for client-side validation.
Additional Learning
If you’re unfamiliar with NuGet or need a refresher, be sure to check out this Treehouse workshop.
Package Management with NuGet If you’d like to learn more about the jQuery Validation plug-in, see their official documentation.
If you’d like to learn more about unobtrusive JavaScript and why it’s important, see this Treehouse blog post.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up