views:

69

answers:

3

I have this code in my view.

After my database adding the message is showing perfectly but my Grid is not showing the updated result. If I keep

return true;

when I click Submit button I am getting popup window immediately and then adding to the database? This showing my updated result in the grid.

+1  A: 

The AJAX request is asynchronous, so it finishes the form submit routine before the callback finishes, which might explain the result you are getting. I think there may be an async setting you can turn off so that the operation/callback must complete first.

Brian
Thanks Brian, how to do that? Can you please tell me?
kumar
Check this out: the first option has an async property, that you set to false. Read more here: http://api.jquery.com/jQuery.ajax/ Be aware that could have repercussions though. You may want to consider refactoring your code to pass the true/false via a callback...
Brian
+1  A: 

Basing on your code and as far as i'm aware of, you have no way on loading the new data.

The reason why when you return true; the new data is being loaded is because the submit event successfully perform it's default operation which is submitting to the server and refreshing the page.

On the other hand, return false; prevents this operation (including the refresh page) and instead just run the XHR.

What you could do is:

Return the new data as a result of the XHR and parse it accordingly.

sheeks06
Please can you tell me what is XHR?
kumar
Xml HTTP Request = AJAX
sheeks06
+1  A: 

If you have return true at the end of your submit function, then the browser will go to the url in the action attribute of the form. I think you should always return false if you have taken care of the form data in the ajax call. You need to get the updated grid as part of the ajax success method.

You can't have the ajax popup and still let the form submit the normal way.

$(function () {
          $('#form4').submit(function () {
              ...    
              $.ajax({
                 ...
                  success: function (result) {
                      ...
                      // update grid
                      grid_container = $("#grid").html('');
                      $("<table>").appendTo(grid_container);
                      for (ii = 0; ii < result.grid.length; ++ii) {
                         tr = $("<tr>").appendTo(table);
                         for (jj=0; jj < result.grid[ii].length; ++ii) {
                             td = $("<td>").text(result.grid[ii][jj]).appendTo(tr);
                         }
                      }

                      // show success message
                      alert('Saved NewServiceTypeCategory Successfully. Thank you!');
                  }
              });
              return false;
          });
      });

Now all you have to do is make your server side post handler return the XML with the grid data.

MattSmith
Thanks MattSmith how to show the Success message at the end?
kumar