MVC3 & JQuery Dialog won't Submit Form when Opening the Dialog

I have a dialog that is used for searching in an MVC3 application. The Search button on the dialog posts to an MVC3 Controller action that returns JSON of search results which then get parsed into an HTML table. All of that works fine when the user clicks the Search button on the dialog.

However, in some circumstances, I want the search to kick off automatically as soon as the dialog opens. Unfortunately, this doesn't work. The user has to physically click on the Search button in order to initiate the Search.

My code looks like such:

$('#RepSearchDialog').dialog({
          autoOpen: true,
          width: 1050,
          height: 500,
          resizable: false,
          title: 'Rep Search',
          modal: true,
          open: function () {
            $('.ui-dialog-titlebar').hide();
            $('#RepSearchStoreId').val($('#StoreId').val());

            // This part doesn't work, not sure why
            //RepSearchDialog_SearchForReps();
          }
        });

The search button has JS like this:

 $('#RepSearchButton').click(function (e) {
      RepSearchDialog_SearchForReps();
    });

And the RepSearchDialog_SearchForReps looks like this:

function RepSearchDialog_SearchForReps() {
    $('#RepSearchForm').submit(function () {
      $.ajax({
        url: this.action,
        type: "POST",
        cache: false,
        dataType: 'text json',
        data: $(this).serialize(),
        success: RepSearchDialog_SearchForRepsComplete,
        error: function (request, status, error) {
          alert("An error occurred submitting the form. \r\n\r\n Error: " + request.responseText);
        }
      });
      return false;
    });
  }

  function RepSearchDialog_SearchForRepsComplete(response, status, xhr) {
    $('#RepSearchButton').unbind(); // NECESSARY, or you will get stacked calls!!
    if (status == "error") {
      alert('failed');
    }
    else {
      LoadRepSearchResults(response);
    }
  }

The RepSearchDialog_SearchForReps call simply makes an AJAX call to the MVC3 controller and appends the returning values to an HTML table in a DIV hosted inside the Dialog. When the user clicks the Search button all of this works. But trying to kick this off automatically in the OPEN function does not. Any clues why?

Answers


The reason that your search button is working is that it is still using the normal post as well as calling your javascript. You need to change it to this:

$('#RepSearchButton').click(function (e) {
    e.preventDefault();
    RepSearchDialog_SearchForReps();
});

Here the e.preventDefault() will stop the native submit event from occurring.

Another problem is your RepSearchDialog_SearchForReps. The way you have it set up, you are binding the $.ajax call to a submit event every time RepSearchDialog_SearchForReps is called. You have two options for this. You can simply do this:

function RepSearchDialog_SearchForReps() {
    var form = $('#RepSearchForm');
    $.ajax({
        url: form.attr('action'),
        type: "POST",
        cache: false,
        dataType: 'text json',
        data: form.serialize(),
        success: RepSearchDialog_SearchForRepsComplete,
        error: function (request, status, error) {
          alert("An error occurred submitting the form. \r\n\r\n Error: " + request.responseText);
        }
    });
}

or this

// this goes in your document ready code elsewhere...
$(function() {
    $('#RepSearchForm').submit(function () {
        $.ajax({
            url: this.action,
            type: "POST",
            cache: false,
            dataType: 'text json',
            data: $(this).serialize(),
            success: RepSearchDialog_SearchForRepsComplete,
            error: function (request, status, error) {
              alert("An error occurred submitting the form. \r\n\r\n Error: " + request.responseText);
            }
        });
        return false;
    });
});

function RepSearchDialog_SearchForReps() {
    $('#RepSearchForm').submit();
}

Need Your Help

Using the ShadowRoot of a component onAttach

dart angular-dart

So I have this angular dart component:

How to close a system dialog on a BlackBerry?

blackberry dialog integration

I have a BlackBerry app running in the background that needs to know when a "Missed call" system dialog is brought up by the system, and programmatically close it without user intervention. How can...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.