问题
I'm having problems validating 2 textboxes using the [Compare-attribute. Even if they're same the client validation says they're different.
In my editmodel I have:
[Required]
[DataType(DataType.Password)]
public string Password { get; set; }
[DataType(DataType.Password)]
[Compare("Password", ErrorMessage = "Password not matching")]
public string ConfirmPassword { get; set; }
In view:
<tr>
<td class="editor-label">
@Html.LabelFor(model => model.User.Password):
</td>
<td class="editor-field">
@Html.EditorFor(model => model.User.Password)
@Html.ValidationMessageFor(model => model.User.Password)
</td>
</tr>
<tr>
<td class="editor-label">
@Html.LabelFor(model => model.User.ConfirmPassword):
</td>
<td class="editor-field">
@Html.EditorFor(model => model.User.ConfirmPassword)
@Html.ValidationMessageFor(model => model.User.ConfirmPassword)
</td>
</tr>
Textboxes gets rendered as:
<input class="text-box single-line password" data-val="true"
data-val-required="Password missing" id="User_Password"
name="User.Password" type="password" value="" />
<input class="text-box single-line password" data-val="true"
data-val-equalto="Password not matching"
data-val-equalto-other="*.Password" id="User_ConfirmPassword"
name="User.ConfirmPassword" type="password" value="" />
<span class="field-validation-valid" data-valmsg-for="User.ConfirmPassword"
data-valmsg-replace="true"></span>
What might be wrong/missing here?
回答1:
I updated unobtrusive + jquery validation using the Nuget packagemanager. And that did the trick
来源:https://stackoverflow.com/questions/20002074/mvc-compare-not-working