Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC HTML helper methods for new HTML5 input types

HTML5 appears to support a new range of input fields for things such as:

  • Numbers
  • Email addresses
  • Colors
  • URLs
  • Numeric range (via a slider)
  • Dates
  • Search boxes

Has anyone implemented HtmlHelper extension methods for ASP.NET MVC that generates these yet? It's possible to do this using an overload that accepts htmlAttributes, such as:

Html.TextBoxFor(model => model.Foo, new { type="number", min="0", max="100" }) 

But that's not as nice (or typesafe) as:

Html.NumericInputFor(model => model.Foo, min:0, max:100) 
like image 767
Drew Noakes Avatar asked Aug 31 '10 20:08

Drew Noakes


People also ask

What is strongly typed HTML helpers in MVC?

The Strongly-Typed HTML helper (i.e., NumericTextBox) takes lambda as a parameter that tells the helper, which element of the model to be used in the typed view. The Strongly typed views are used for rendering specific types of model objects, instead of using the general ViewData structure.

How can create HTML helper in ASP.NET MVC?

There are two ways in MVC to create custom Html helpers as below. We can create our own HTML helper by writing extension method for HTML helper class. These helpers are available to Helper property of class and you can use then just like inbuilt helpers. Add new class in MVC application and give it meaningful name.

What is the use of helper class in ASP NET MVC?

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.

Does HTML5 support input fields for MVC?

HTML5 appears to support a new range of input fields for things such as: Has anyone implemented HtmlHelper extension methods for ASP.NET MVC that generates these yet?

What are the different types of built-in HTML helpers offered by ASP NET?

ASP.NET provides a wide range of built-in HTML helpers that can be used as per the user’s choice as there are multiple overrides available for them. There are three types of built-in HTML helpers offered by ASP.NET. 1. Standard HTML Helper

Which class can create HTML controls programmatically in MVC?

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.


1 Answers

Just a heads up that many of these are now incorporated into MVC4 by using the DataType attribute.

As of this work item you can use:

public class MyModel  {     // Becomes <input type="number" ... >     public int ID { get; set; }      // Becomes <input type="url" ... >     [DataType(DataType.Url)]     public string WebSite { get; set; }      // Becomes <input type="email" ... >     [DataType(DataType.EmailAddress)]     public string Email { get; set; }      // Becomes <input type="tel" ... >     [DataType(DataType.PhoneNumber)]     public string PhoneNumber { get; set; }      // Becomes <input type="datetime" ... >     [DataType(DataType.DateTime)]     public DateTime DateTime { get; set; }      // Becomes <input type="date" ... >     [DataType(DataType.Date)]     public DateTime Date { get; set; }      // Becomes <input type="time" ... >     [DataType(DataType.Time)]     public DateTime Time { get; set; } } 
like image 184
Paul Hiles Avatar answered Sep 18 '22 15:09

Paul Hiles