Ridiculously easy AJAX with ASP.NET MVC and jQuery
- by eddraper
After deciding I wanted to dive full-on into the world of ASP.NET MVC 2, I began doing some research into what would be the best way to support some of my required AJAX functionality on this platform. The result of these efforts was a barrage of options – many of which required completely different JScript infrastructure than what I planned to go forward with. As I’ve been delighted with jQuery so far, I began tossing out all approaches that didn’t natively leverage it… Thus, I planned to resist the temptation to take anymore <script> dependencies whatsoever, unless I thoroughly proved that jQuery could NOT do what I planned to do.
Here’s some code I wish I would’ve found early in my research. This would’ve saved me quite a bit of time and search engine bandwidth. ;-)
<script type="text/javascript">
$(document).ready(function () {
$('#div_name_here').load('<%=Url.Action("ACTION_NAME_HERE","CONTROLLER_NAME_HERE")%>');
$('#id_of_link_I_want_trigger_the_ajax_call')
.bind('click', function (event) {
$('#div_name_where_I_want_to_have_the_ajax_response_loaded_here').load('<%=Url.Action("ACTION_HERE","CONTROLLER_HERE", )%>');
})
})
</script>