Loading partial view in ASP.Net MVC using jQuery - which method is preferred?

In very recent issues, I had problems with this. My code is:

$("#SearchResults").load("/Invoice/InvoiceSearchResults/"); 

And I was advised to use this instead:

 $.ajax({ url: "/Invoice/InvoiceSearchResults/", type: 'GET', dataType: 'html', // <-- to expect an html response success: doSubmitSuccess }); 

from:

 function doSubmitSuccess(result) { $(".SearchResults").html(result); 

}

And then someone kindly tried to help me:

 $.get(postUrl, function(data) { $("#posts").append(data); $('#ajaxLdMore').addClass('hideElement'); $('#ldMore').removeClass('hideElement'); }); 

Turns out my problem was that I'm an idiot. abd used the selector "#" instead of "."

I'm just wondering if I should change my code to any of them?

Are there real pros and cons of each approach or preference?

Is there a better way that no one has posted yet?

I am not trying to open a huge debate (I do not think :)). I just want to understand a little more about this.

thanks

+4
source share
2 answers

Both will work, but I personally rely on the jQuery solution as it is more durable than Microsoft AJAX solution. The transition to jQuery, even with the help of MS, is very obvious, since they hugged the library very much.

+3
source

Using $ .ajax gives you more flexibility. I find this particularly useful for error handling.

In addition, you can combine this into one block of code if you are not reusing doSubmitSuccess elsewhere

 $.ajax({ url: "/Invoice/InvoiceSearchResults/", type: 'GET', dataType: 'html', // <-- to expect an html response success: function(result){ $(".SearchResults").html(result); }, error: function(err){ //handle errors here } }); 
+2
source

All Articles