0

I know this is a common problem. I've been looking at the various solutions proposed on this site and elsewhere but cannot hit upon something that works in my situation.

Here is my script:

$(function () {

    $('a.editPersonLink').live("click", function (event) {
        loadDialog(this, event, '#personList');
        $.validator.unobtrusive.parse('#editPersonContainer');
    });

    $(".deleteButton").live("click", function(e) {
        e.preventDefault();
        var $btn = $(this);
        var $msg = $(this).attr("title");

        confirmDelete($msg, 
            function() {
                deleteRow($btn, $target);
            });
    });

});

function loadDialog(tag, event, target) {
    event.preventDefault();
    var $loading = $('<img src="../../Content/images/ajaxLoading.gif" alt="loading" class="ui-loading-icon">');
    var $url = $(tag).attr('href');
    var $title = $(tag).attr('title');
    var $dialog = $('<div></div>');
    $dialog.empty();
    $dialog
        .append($loading)
        .load($url)
        .dialog({
            autoOpen: false
            ,title: $title
            ,width: 800
            ,modal: true
            ,minHeight: 200
            ,show: 'slide'
            ,hide: 'clip'
        });

    $dialog.dialog( "option", "buttons", {
        "Save": function () {
            var dlg = $(this);
            $.ajax({
                url: $url,
                type: 'POST',
                data: $("#formData").serialize(),
                success: function (response) {
                    $(target).html(response);
                    dlg.dialog('close');
                    dlg.empty();
                    $("#ajaxResult").hide().html('Record saved').fadeIn(300, function () {
                        var e = this;
                        setTimeout(function () { $(e).fadeOut(400); }, 2500);
                    });
                },
                error: function (xhr) {
                    if (xhr.status == 400) 
                        dlg.html(xhr.responseText, xhr.status);     /* display validation errors in edit dialog */
                    else 
                        displayError(xhr.responseText, xhr.status); /* display other errors in separate dialog */

                }
            });
        },
        "Cancel": function() { 
            $(this).dialog("close");
            $(this).empty();
        }
    });

    $dialog.dialog('open');
};

Right up near the top I am trying to cause the form to recognise the validation from the partial view on the dialog via the statement:

$.validator.unobtrusive.parse('#editPersonContainer');

editPersonContainer is the name of the div containing the data in the partial view loaded into the dialog.

The bottom line is that the validation is not recognised. Am I making the call to validator.unobtrusive.parse in the wrong place, or am I missing something else here?

1
  • Is there a <form> tag in the jqui dialog? What do the rendered <input> elements look like? Do they have data-val="true" attributes? Commented Jan 19, 2012 at 8:33

2 Answers 2

1

I ended up changing my script to use the techniques described here

Validation now works on my jQuery UI dialogs.

Sign up to request clarification or add additional context in comments.

1 Comment

In relation to the link that I posted above as my solution to this problem note that in order to allow the dialog to work when called multiple times I had to add in a call to $this).empty() after the call to $(this).Dialog('Close') - and a call to $(dialog).empty() after the call to $(dialog).dialog('close'). Without this I had major problems if the dialog was called twice without the calling page being refreshed.
0

Hi I came up to your question as I was looking for the same.

I include this before the ajax call to validate on client side:

if (ModelState.IsValid) {}

I make an article with the full project on

Validation client/Server JqueryUI Dialog

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.