以下是原因。這是生成的HTML:即使密碼相同,比較驗證器也始終關閉
<div>
<label for="RegisterModel_Password">Contraseña</label>
<input class="text-box single-line password" data-val="true" data-val-length="Su Contrase&#241;a debe tener al menos 6 caracteres." data-val-length-max="100" data-val-length-min="6" data-val-required="Debe escribir su contrase&#241;a" id="RegisterModel_Password" name="RegisterModel.Password" type="password" value="" />
<span class="field-validation-valid" data-valmsg-for="RegisterModel.Password" data-valmsg-replace="true"></span>
</div>
<div>
<label for="RegisterModel_ConfirmPassword">Confirme Su Contraseña</label>
<input class="text-box single-line password" data-val="true" data-val-equalto="Sus contrase&#241;as no son las mismas." data-val-equalto-other="*.Password" id="RegisterModel_ConfirmPassword" name="RegisterModel.ConfirmPassword" type="password" value="" />
<span class="field-validation-valid" data-valmsg-for="RegisterModel.ConfirmPassword" data-valmsg-replace="true"></span>
</div>
注意到這一點,在確認密碼框中:
data-val-equalto-other="*.Password"
這應該是RegisterModel.Password
,因爲我猜的JavaScript看起來像一個名爲「RegisterModel輸入.Password「,不是?
這裏是我的模型代碼:
[Required(ErrorMessage = "Debe escribir su contraseña")]
[StringLength(100, ErrorMessage = "Su {0} debe tener al menos {2} caracteres.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Contraseña")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirme Su Contraseña")]
[Compare("Password", ErrorMessage = "Sus contraseñas no son las mismas.")]
public string ConfirmPassword { get; set; }
上爲什麼發生這種情況的任何想法?
剛剛創建了一個全新的應用MVC3,這是模型:
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
和驗證工作。這裏唯一的區別是它們直接在視圖中使用模型,而我傳遞一個* 查看 *包含該模型的RegisterModel。
這個工作默認的HTML是不同的,雖然到我的:
<div class="editor-field">
<input data-val="true" data-val-length="The Password must be at least 6 characters long." data-val-length-max="100" data-val-length-min="6" data-val-required="The Password field is required." id="Password" name="Password" type="password" />
<span class="field-validation-valid" data-valmsg-for="Password" data-valmsg-replace="true"></span>
</div>
<div class="editor-label">
<label for="ConfirmPassword">Confirm password</label>
</div>
<div class="editor-field">
<input data-val="true" data-val-equalto="The password and confirmation password do not match." data-val-equalto-other="*.Password" id="ConfirmPassword" name="ConfirmPassword" type="password" />
<span class="field-validation-valid" data-valmsg-for="ConfirmPassword" data-valmsg-replace="true"></span>
</div>
只是確認這由什麼原因引起我懷疑。我把所有東西都移到了一個模型中,而不是ViewModel(正如我應該正確的那樣),jQuery驗證庫確實能夠正常工作。我不知道這是否是一個錯誤? –