DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Frameworks
  4. ASP.NET MVC HTML Helpers

ASP.NET MVC HTML Helpers

Dane Morgridge user avatar by
Dane Morgridge
·
Aug. 25, 10 · Interview
Like (0)
Save
Tweet
Share
6.38K Views

Join the DZone community and get the full member experience.

Join For Free
One of the things I love about ASP.NET MVC is the fact that I get to work with bare-metal HTML. With Webforms we got the ease of use of just dropping a control on a form, but the HTML generated was less that optimal. Depending on the application you were doing, it was worth the trade off. Webforms put all kinds of bloat in your HTML that was required to support the Webforms way of doing things. This is one of the major reasons I have moved almost completely to MVC. Although MVC does makes things easier to test and give you a greater separation of concerns, this was all technically achievable with Webforms, it just took more work to get there. The Webforms bloat caused a lot of issues like increased page weight and sometimes it was difficult to ensure that the generated HTML was CSS friendly.

Webforms controls may make it quicker to get a UI built, but you don't to sacrifice some of that flexibility when working with ASP.NET MVC. Instead of having to hand craft all of your HTML, you can use HTML helpers. HTML helpers are methods that build HTML tags based on parameters you pass in. Instead of building a HTML textbox by hand like this:
<input type="text" name="firstname" id="firstname">
You can do this:
<% Html.TextBox("firstname"); %>
The TextBox method has multiple parameters including passing in the value of the textbox and other style options. You might be thinking, there isn't much difference in code between the two. In this simple example, either options works. One thing you will notice is that in the top HTML block, I am also setting the id as well as the name. This helps greatly when using jQuery and the helper does that for you. But lets think that you are building a textbox that contains a value from your model. You could build the HTML like this:
<input type="text" name="firstname" id="firstname" value="<%= Model.FirstName %>">
Or you could do this using a helper:
<% Html.TexBox("firstname", Model.FirstName); %>
Both methods will produce the same HTML, but the second one is more flexible. Let's say for instance, you have a business requirement change (like that ever actually happens...) and that textbox now needs to be a text area. Not likely that this would happen with a first name type property, but bare with me. You will have to change your HTML to this:
<textarea name="firstname" id="firstname"><%= Model.FirstName %></textarea>
Or you can simply change the helper method you are using:
<% Html.TextArea("firstname", Model.FirstName); %>
I have personally had to do this fairly often. When the business requirements change as frequently as they do sometimes, it pays to be flexible. If you need to set any custom attributes on the HTML tag you can also do that with the helper by either passing in a dictionary or using an anonymous type:
<% Html.TextArea("firstname", Model.FirstName, new { parameter = "value" }); %>
HTML helpers can certainly make things easier and save you some time when building your UI. They don't have the bloat that comes with Webforms controls, but have a great deal of flexibility. Using extension methods you can also create your own if you need something special or just need to tweak the way they get rendered.
HTML ASP.NET MVC ASP.NET

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How Elasticsearch Works
  • Distributed Tracing: A Full Guide
  • mTLS Everywere
  • What Are the Benefits of Java Module With Example

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: