2012-09-04 54 views
18

我在表單中使用ASP.NET MVC 3 TextBoxFor並希望使用type =「email」爲至少一些移動設備更容易輸入,但無法找到如何使用TextBoxFor設置它。這不容易嗎?是否有可能設置類型的輸入生成的TextBoxFor

在查看

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

在模型

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

(已使用數據標註,以保護數據庫大小限制)

+0

可能重複http://stackoverflow.com/問題/ 5272869/mvc-html5-email-tag) –

+1

我單獨詢問是因爲我不關心電子郵件驗證問題,並回答這個問題,同時詳細討論了獲取類型電子郵件輸入的根本問題。 – d456

回答

2

嘗試增加[DataType(DataType.EmailAddress)]到電子郵件屬性。

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

沒有改變任何東西。 – d456

+0

您需要使用'@ Html.EditorFor'才能正常工作。 – Ruchan

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

    } 

嘗試添加這一點。我認爲它有效

+0

沒有改變任何東西。 – d456

3

你用錯了!

使用EditorFor在查看:

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

在模型中,添加[DataType(DataType.EmailAddress)]數據註釋屬性:

[DataType(DataType.EmailAddress)] 
public string Email { get; set; } 
[MVC HTML5 EMAIL標籤(的
相關問題