From the course: ASP.NET MVC: Building for Productivity and Maintainability
Unlock the full course today
Join today to access over 22,500 courses taught by industry experts or purchase this course individually.
Creating custom HTML helpers to reduce and reuse markup
From the course: ASP.NET MVC: Building for Productivity and Maintainability
Creating custom HTML helpers to reduce and reuse markup
- [Instructor] In this chapter, I've already shown you how to use extension methods to really help clean up your code and in this video, I'll show you how to create your own HTML helpers. You know those methods that hang off the @hmtl property in razor views that help you generate form fields? Just like the ones I'm showing here. Since the asp.net MVC framework already offers HTML helpers for generic tasks like generating form fields, I'm going to look for something a little more specific to my application, open up the Product view, like the stars rating that's displayed in the Product view and on the ProductList view as well. The cool thing about the HTML property is that it's an object, an object that you can attach extension methods to and that means creating your own custom HTML helper is as simple as creating an extension method on the HTML helper class. So, to convert this markup into an HTML helper the first I'll do is create a static class to hold these new extension methods…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
Using extension methods to remove magic strings5m 30s
-
Using extension methods to strongly type common actions4m 38s
-
Creating custom HTML helpers to reduce and reuse markup6m 16s
-
Using child actions to generate complex common markup4m 59s
-
Challenge: Use a child action to make a smarter HTML helper1m 29s
-
Solution: Use a child action to make a smarter HTML helper4m 16s
-
-
-
-
-
-