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

Creating custom HTML helpers to reduce and reuse markup

From the course: ASP.NET MVC: Building for Productivity and Maintainability

Start my 1-month free trial

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…

Contents