28

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)

2
  • possible duplicate of MVC HTML5 EMAIL tag Commented Sep 4, 2012 at 15:55
  • 1
    I asked separately because I did not care about the email validation that the answer on that question goes into while glazing over what was my root problem of getting an input of type email. Commented Sep 4, 2012 at 18:00

4 Answers 4

59

Try to use

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

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

Sign up to request clarification or add additional context in comments.

1 Comment

Don't know why I wrote this off and didn't just try it. Thanks.
3

You're using it the wrong way.

Use EditorFor in View:

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

In model, add the [DataType( DataType.EmailAddress )] Data Annotations property:

[DataType( DataType.EmailAddress )]
public string Email { get; set; }

Comments

2

Try adding [DataType( DataType.EmailAddress )] to the email property.

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

Comments

1
[StringLength(50)]
[DataType(DataType.EmailAddress, ErrorMessage = "Invalid Email Address")]
public string EmailAddress { get; set; }

Try to add this. I think it works.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.