MVC Compare not working

痞子三分冷 提交于 2019-12-13 04:49:21

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!