Site icon Ryadel

ASP.NET MVC - How to set HtmlAttributes programmatically in Razor HTML helpers

ASP.NET MVC - Aggiungere uno o più percorsi di ricerca predefiniti per le View

If you're a seasoned ASP.NET MVC developer, you most likely already know that most Razor HTML helpers let you use an anonymous object in a parameter called htmlAttributes. As the name says, its members will be used to generate the HTML attribures of that element.

For example, this:

Will become this:

The only two notable exceptions are Html.EditorFor and Html.DisplayFor, which uses a slightly different syntax:

As we can see, even though they're using an anonymous object into an anonymous object container, the overall logic is basically the same.

It's worth noting that this "enhanced" capability for the two above methods has been added in ASP.NET 5.1 to ease the life of most developers. For the sake of simplicity, we'll stick to this post topic and avoid explaining this specific behaviour: to read the whole story, we strongly suggest to read this official MS post. For now, let's just assume that we're all using ASP.NET MVC 5.1 (or above).

As you probably know, anonymous types are created by the compiler at runtime level, so they aren't exactly handy when we want to modify them at execution level...  to put it in other words, it just cannot be done.

The Problem

So, what if we need to assign these HTML attributes programmatically and dinamically?

As a matter of fact, we can do a lot of things using variables, inline methods, conditional blocks or other viable strategies:

However, there are scenarios where we need to conditionally add (or skip) certain attributes as a whole. The perfect example of this is a TextBoxFor element which we would like to set readonly only in some circumstances. The readonly attribute is a tricky one, because it works as a property setting and not as a valued attribute. Its mere presence is enough to set the element read-only, despite of its value: this basically means that there's no way to explicitly set it to FALSE.

Pretty neat, isn't it? To know more about it, read here.

For the above reason, we cannot do something like this:

The Solution

There are a lot of viable workarounds around the web: most of them are collected in this StackOverflow threads. However, no one of the listed one was DRY enough for my tastes. For this very reason, I did some research and eventually I came up writing this simple helper method:

The above technique allows me to deal with this issue just the way I wanted to, without having to repeat a single line of code.

It can be used in the following way:

Conclusion

That's it for now: I hope that the above method will help most ASP.NET MVC developers to deal with their htmlAttributes!

 

Exit mobile version