Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to set type of input generated by TextBoxFor

I am using ASP.NET MVC 3 TextBoxFor in a form and would like to use type="email" for easier input for at least some mobile devices but cannot find how to set it using TextBoxFor. Is this not easily possible?

In View

@Html.LabelFor(m => m.Email) @Html.TextBoxFor(m => m.Email) 

In model

[StringLength(50)] public string Email { get; set; } 

(Already using a data annotation to protect size constraint in DB)

like image 407
d456 Avatar asked Sep 04 '12 15:09

d456


People also ask

What is the difference between TextBox and TextBoxFor in MVC?

IMO the main difference is that Textbox is not strongly typed. TextboxFor take a lambda as a parameter that tell the helper the with element of the model to use in a typed view. You can do the same things with both, but you should use typed views and TextboxFor when possible.

What is TextBoxFor in MVC?

TextBoxFor<TModel,TProperty>(HtmlHelper<TModel>, Expression<Func<TModel,TProperty>>, Object) Returns a text input element for each property in the object that is represented by the specified expression, using the specified HTML attributes.

How do you make a TextBoxFor ReadOnly?

The TextBoxes can be made ReadOnly by setting the HTML ReadOnly attribute using the HtmlAttributes parameter in Html. TextBox and Html. TextBoxFor helper functions.

What is the difference between EditorFor and TextBoxFor?

TextBoxFor will always show the textbox element no matter which kind of property we are binding it with. But EditorFor is much flexible in this case as it decides which element will be more suitable to show the property.


1 Answers

Try to use

@Html.TextBoxFor(m => m.Email, new { @type = "email" }) 

http://msdn.microsoft.com/en-us/library/ee703538.aspx (htmlAttributes)

like image 55
Xordal Avatar answered Oct 02 '22 21:10

Xordal