Low impact Ajax with jQuery and ASP.NET MVC

Here's a simple way to avoid postbacks in your site, still be SEO friendly and degrade gracefully to JavaScript free browsers (and handle middle clicks and copy&paste) in ASP.NET MVC using jQuery.

First, we'll create an alternate MasterPage called No.master with no content, only one ContentPlaceHolder:

<asp:ContentPlaceHolder ID="MainContent" runat="server" />

Now, we're able to tell via HTTP Headers if a Request is an Ajax Request, so we'll write a new method in our Controller Base class to toggle the master page:

protected string GetMasterPageName() { if (this.IsAjaxRequest()) return "No"; return "Site"; }

And now, whenever we return a View in our Controllers, we'll use the overload that takes in the name of a View

return View("Edit", GetMasterPageName(), postServices.GetByID(id))

So when a request comes in normally, we are returned full mark-up.  If the request is AJAX, we only return HTML for that view.

We won't modify the url of our links, however we'll give them all a class so we can grab then in jQuery.

<a href="/Post/Show/<%=post.ID %>" class="ajaxLink"><%= post.Subject %></a>

Then we'll wire a method called displayLink to all our ajaxLinks' click event handlers:

$('.ajaxLink').click(displayLink);

And in that function we'll make an AJAX call to the link's target (its href attribute value) using the jQuery $.get method, update our main content area and return false in order to cancel the click event of the link:

displayLink = function() { $.get($(this).attr('href'), function(response) { $('#mainContent').html(response); }); return false; };

And that's it.  I would (and did) refactor the JavaScript and add a loading graphic, but I'm just awestruck at how you can Ajaxify an application in ~12 lines of code.

Have I mentioned loving Extension Methods?

Here's my new favorite:

public static string AppSetting(this string parameter) { return System.Configuration.ConfigurationManager.AppSettings[parameter]; }

Now I can simply call

"BaseUrl".AppSetting();

 

Beautiful.

Writing a jQuery plugin to filter a table on keypress
I recently had a requirement to filter a table as the user was typing.

I was able to write a jQuery plugin to do this in just six lines of code.

I want to use jQuery to add a keyup handler to my search box, and target table(s) via a selector parameter.

First, in order to extend functionality onto a selector, we add a new function to $.fn


  $.fn.tableFilter = function(tableSelector) {


Then we want to get a reference to the table we're going to filter:

    table = $(tableSelector);


and we'll write a function that will actually perform the filtering:

  

    updateTable = function() {

 

now, on every keypress we need a blank slate.  lets hide all the rows besides the header:


        table.find('tr:gt(0)').hide();

then, we'll display the rows that match the text we've typed:


        table.find('td:contains("' + $(this).val() + '")').parents('tr').show();

    }


and we'll assign the keypress of event of our textbox to our new filterfing function: