Trouble getting unobtrusive validation working wit

2019-01-23 05:28发布

I have having a lot of trouble getting validation to work on MVC3. It works fine when I just load the page directly, but it does not validate when I use jquery AJAX POST:

This form is loaded using $('#modal-dialog').load('/DartsMVC/Restaurant/Edit/13'), This is the rendered HTML:

<form action="/DartsMVC/Restaurant/Edit/13" method="post">
    <fieldset>
        <legend>Restaurant</legend>

        <input data-val="true" data-val-number="The field RestaurantID must be a number." data-val-required="The RestaurantID field is required." id="RestaurantID" name="RestaurantID" type="hidden" value="13">

        <div class="editor-label">
            <label for="Name">Name</label>
        </div>
        <div class="editor-field">
            <input class="text-box single-line" data-val="true" data-val-required="The Name field is required." id="Name" name="Name" type="text" value="furaibo">
            <span class="field-validation-valid" data-valmsg-for="Name" data-valmsg-replace="true"></span>
        </div>

        <p>
            <input type="submit" value="Save" class="ui-button ui-widget ui-state-default ui-corner-all" role="button" aria-disabled="false">
        </p>
    </fieldset>
</form>

AJAX POST is used by intercepting the form/submit. The modal dialog pops up and closes itself after the post completes. I would like to validate the form before sending the post:

// force change the submit data to an ajax POST (usually 'save', 'delete' button)
$('#modal-dialog').delegate('form', 'submit', function (e) {
    e.preventDefault();
    var form = $(this);
    form.validate(); // does nothing
    if (form.valid()) {
        $.ajax({
            url: form.attr('action'),
            type: "POST",
            data: form.serialize(),
            success: function (data) {
                $('#modal-dialog').dialog('close');
            },
            error: function (jqXhr, textStatus, errorThrown) {
                alert("Error '" + jqXhr.status + "' (textStatus: '" + textStatus + "', errorThrown: '" + errorThrown + "')");
            }
        });
    }
});

Did I break something by doing this? .valid() always returns true.

My web.config has:

  <appSettings>
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>

My page source has:

<link href="/DartsMVC/Content/themes/cupertino/jquery-ui-1.8.14.custom.css" rel="stylesheet" type="text/css">

<script src="/DartsMVC/Scripts/modernizr-1.7.min.js" type="text/javascript"></script>
<script src="/DartsMVC/Scripts/jquery-1.5.1.min.js" type="text/javascript"></script>
<script src="/DartsMVC/Scripts/jquery-ui-1.8.14.custom.min.js" type="text/javascript"></script>

<script src="/DartsMVC/Scripts/jquery.unobtrusive-ajax.js" type="text/javascript"></script>
<script src="/DartsMVC/Scripts/jquery.validate.js" type="text/javascript"></script>
<script src="/DartsMVC/Scripts/jquery.validate.unobtrusive.js" type="text/javascript"></script>

And my model has:

    [Required]
    public string Name { get; set; }

After more research, I think it may have something to do with dynamic controls:

1条回答
成全新的幸福
2楼-- · 2019-01-23 06:08

Solved

Calling $.validator.unobtrusive.parse($('form')) did the trick. I guess the validator needs to be recreated for dynamic content

查看更多
登录 后发表回答