2011-05-18 32 views
10

而不是直接使用對象,在一個簡單的剃​​刀視圖我有一個窗體使用,因爲它+ s model一個裝飾器對象。模型不適用DataType.Password

public class GlobalAccount 
{ 
    public GlobalAccount() 
    { 
     this.TestService = new TestServiceModel(); 
    } 

    public TestServiceModel TestService { get; set; } 
} 

beeing TestServiceModel表示

public class TestServiceModel 
{ 
    [Required] 
    [Display(Name = "Endpoint (url of your service like http://mydomain/remote/)")] 
    public string Endpoint { get; set; } 

    [Required] 
    [Display(Name = "System username")] 
    public string UserName { get; set; } 

    [Required] 
    [DataType(DataType.Password)] 
    [Display(Name = "System password")] 
    public string Password { get; set; } 
} 

注意Password財產與DataType.Password

,並在Partial Razor view裝飾我有一個調用該對象

@model OnlineServices.Administration.Models.GlobalAccount 
... 
@Html.TextBoxFor(model => model.TestService.Password, new { size = "30" }) 

的問題是,在html我得到的type="text"代替text="password"

可以在this image看到整個流程:

enter image description here

我是什麼失蹤在這裏嗎?

回答

19

您應該使用Html.Password,甚至更好的使用Html.EditorFor這將讀取DataType.Password和輸出密碼類型的輸入你。

Html.TextBoxFor只是基於文本的輸入,而不是基於密碼的輸入。

+0

愚蠢的我......這麼簡單,但如此遙遠!我只是忽略它......總是在考慮模型中的錯誤,並且從來沒有親眼看到: -/**我需要休假!**:o) – balexandre 2011-05-18 12:54:35

+1

很容易錯過。 – Kieron 2011-05-18 13:08:13

+2

請考慮使用Html.PasswordFor(請參閱下面的@Skuld答案) – 2012-02-10 22:56:00

14

除了Kieron的回答,在MVC3中,使用Html.EditorFor作爲密碼輸入並不是更好,因爲某些原因,如果服務器返回頁面(比如用戶名密碼組合不正確),那麼使用EditorFor,密碼被傳回到頁面(並且查看源密碼可見)

當使用Html.PasswordFor密碼時,不會將密碼傳回客戶端。

+1

服務器仍然返回MVC 5中的密碼。另一個解決方法是繼續並使用'DataType.Password'和'Html.EditorFor',但具有控制器在返回視圖之前清除密碼(如'viewModel.Password = null')。 – Theophilus 2014-09-26 18:03:38