Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting maxlength and other html attributes using ASP.NET MVC helper methods

Tags:

asp.net-mvc

Currently when I want to set html attributes like maxlength and autocomplete, I have to use the following syntax:

<%= Html.TextBox("username", ViewData["username"], new { maxlength = 20, autocomplete = "off" }) %>

Is there any way to do this without having to explicitly set the ViewData["username"] portion? In other words, I want to rely on the helper method's automatic loading routine rather than having to explicitly tell it which field to load up from the ViewData.

like image 978
Kevin Pang Avatar asked Jan 28 '09 10:01

Kevin Pang


People also ask

What is MVC HTML helpers and its methods?

What is HTML Helper in ASP.NET MVC 5? HTML Helpers are methods that return a string. Helper class can create HTML controls programmatically. HTML Helpers are used in View to render HTML content. It is not mandatory to use HTML Helper classes for building an ASP.NET MVC application.

How do I set the MaxLength for HTML TextBoxFor in MVC?

The MaxLength for TextBoxes is set using the HTML MaxLength attribute using the HtmlAttributes parameter in Html. TextBox and Html. TextBoxFor helper functions.

How many types of HTML helpers are there in MVC?

There are three types of built-in HTML helpers offered by ASP.NET.


1 Answers

Just pass "null" as second parameter:

<%= Html.TextBox("username", null, new { maxlength = 20, autocomplete = "off" }) %>
like image 101
veggerby Avatar answered Oct 08 '22 03:10

veggerby