Extend MVC LabelFor Method

Home » Server Side Programming » Web Development » Extend MVC LabelFor Method
ASP.NET MVC Logo

I am working with a Umbraco site that has labels that look like this for required fields:

The problem is the standard @Html.LabelFor does not cater for this.

I had a look round the web and couldn’t find the exact code I wanted, as I really wanted to detect if the required data annotation attribute was set and only add the code if so.

The solution is to create an extension method with my own LabelFor method within:

You use it like this:

When browsing around the web, most of the examples I found were fairly trivial and didn’t cover the situation where I could detect the required attribute. The one problem I had with the above code is that if my requirements were more complex, using string.Replace didn’t feel like the correct method. I found this post that used the TagBuilder, which would solve this problem:

http://www.zunisoft.com/2012/02/aspnet-mvc-labelfor-htmlattributes.html

About Phil

I have been working as a software developer since 1983. This blog could have been called "From Fortran 77, C and Cobol to C# in 20 (not so) easy years", but it doesn't sound quite right somehow. Besides I'm talking about what's happened since 2003, not before!

Leave a Reply

Your email address will not be published.


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">




Top Posts & Pages

Categories

Recent Posts

Recent Comments

Archives

Blogroll

  • Mike Cohn's Blog
  • Scott Hanselman's Blog
- mike@mountaingoatsoftware.com

A summary of Mike Cohn’s most popular and engaging blog posts of 2017. [...]

- mike@mountaingoatsoftware.com

Being agile requires team members to overlap work but doing so brings uncertainty with it. Here’s ho [...]

- mike@mountaingoatsoftware.com

Scrum teams have too many meetings is perhaps the most common criticism of Scrum. But is it valid? [...]

- mike@mountaingoatsoftware.com

Here are five key lessons I learned that were instrumental in my career. See if learning them could [...]

- mike@mountaingoatsoftware.com

A team’s sprints should be the same length every sprint. Here are four reasons why. [...]

- Scott Hanselman

I'm a big fan of the Windows Subsystem for Linux. It's real Linux that runs real user-mode [...]

- Scott Hanselman

Someone gave me an Azure IoT DevKit, and it was lovely timing as I'm continuing to learn about [...]

- Scott Hanselman

I was doing some Angular then remembered that the ASP.NET "Angular Project Template" has a [...]

- Scott Hanselman

I'm a big fan of the SunFounder tech kits (https://www.sunfounder.com), and my kids and I have [...]

- Scott Hanselman

My buddy Nate become a Type 1 Diabetic a few weeks back. It sucks...I've been one for 25 years. [...]

Meta