Ajax.BeginForm()和部分视图中模型的验证消息?

时间:2014-10-09 12:13:53

标签: jquery asp.net-mvc-5 ajax.beginform

我通过Ajax在主视图中加载我的局部视图。 我的局部视图如下所示。现在,第一次单击我的服务器代码将激活验证,但在第二次单击时获取客户端验证,我的模型消息显示在视图中。为什么在第二次单击时只发生客户端验证?

<script type="text/javascript">
  function RegisterAjaxEvents() {
    $.validator.unobtrusive.parse('form');
  }
</script>

@using (Ajax.BeginForm("Add", "Account", new AjaxOptions() { UpdateTargetId = "messageid", HttpMethod = "Post", OnSuccess = "RegisterAjaxEvents();" , InsertionMode=InsertionMode.Replace})) { @Html.AntiForgeryToken()
<div>
  @Html.ValidationSummary(true)
  <table>
    <tr>
      <td>
        @Html.LabelFor(model => model.name, new { @class = "label" })
      </td>
      <td>
        @Html.TextBoxFor(m => m.name, new { @class = "textbox" }) @Html.ValidationMessageFor(model => model.name, string.Empty, new { @class = "error" })
      </td>
    </tr>
    <tr>
      <td>
        @Html.LabelFor(model => model.Userid, new { @class = "label" })
      </td>
      <td>
        @Html.TextBoxFor(m => m.Userid, new { @class = "textbox" }) @Html.ValidationMessageFor(model => model.Userid, string.Empty, new { @class = "error" })
      </td>
    </tr>
    <tr>
      <td>
        @Html.LabelFor(model => model.Email, new { @class = "label" })
      </td>
      <td>
        @Html.TextBoxFor(model => model.Email, new { @class = "textbox" }) @Html.ValidationMessageFor(model => model.Email, string.Empty, new { @class = "error" })
      </td>
    </tr>
    <tr>
      <td>

      </td>
      <td>
        <div id="messageid"></div>
        <div>
          <input type="submit" value="Save" class="btn btn-default" />
        </div>
      </td>
    </tr>
  </table>
</div>
}

此外,我尝试了以下工作,但现在第二次点击也不会触发客户端验证。

function RegisterAjaxEvents() {
    alert("I am fired");
    var x = $(this).closest('form');
    x.removeData('validator');
    x.removeData('unobtrusiveValidation');
    $.validator.unobtrusive.parse(x);
    x.validate();
    alert("I am again fired");
}

0 个答案:

没有答案