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.

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Comments

# re: Low impact Ajax with jQuery and ASP.NET MVC
Gravatar It's a good trick, I bookmarked up front.
Thanks for sharing !
Left by cypher on 11/12/2008 11:32 AM
# re: Low impact Ajax with jQuery and ASP.NET MVC
Gravatar On second thoughts, I have trouble implementing this.

Could you please provide a code download ?

Cheers.
Left by cypher on 11/13/2008 3:55 AM
# re: Low impact Ajax with jQuery and ASP.NET MVC
Gravatar Thanks for your comment, this is my new blog and I'll change the style for you my dear reader anytime! Really a good sharing.
Left by forniture tatuaggi on 7/2/2009 1:53 AM
# Amazing
Gravatar Hi !!! we'll use the overload that takes in the name of a View that it could be transfomed into the latest version that is available in the market.
Regards,
Left by online paralegal courses on 7/5/2009 1:03 AM
# re: Low impact Ajax with jQuery and ASP.NET MVC
Gravatar I believe the information covered in the discussion is top notch. I've been doing a research on the subject and your blog just cleared up a lot of questions.
Left by free music download sites on 7/23/2009 1:00 AM
# dggfg
Gravatar Wow its really informative to known about asp.net thanks for informative post
Left by replica purses on 7/23/2009 6:11 AM
# asdfds
Gravatar The blog is really informative and i like to share this info with my friends thanks for post.
Left by porn reviews on 8/5/2009 6:25 AM
# ghfhg
Gravatar Thanks for sharing these info with us! I was reading something similar on another website that i was researching. I will be sure to look around more. thanks...
Left by anti aging on 8/6/2009 4:55 AM
# re: Low impact Ajax with jQuery and ASP.NET MVC
Gravatar I have trouble implementing this.Could you please provide a code download ?
Left by Term Paper Help on 9/5/2009 3:56 AM
# re: Low impact Ajax with jQuery and ASP.NET MVC
Gravatar I completely agree with the above comment, the internet is with a doubt growing into the most important medium of communication across the globe and its due to sites like this that ideas are spreading so quickly.
Left by où jouer roulette en ligne on 11/23/2009 4:17 AM
# re: Low impact Ajax with jQuery and ASP.NET MVC
Gravatar To enable the details of this specific error message to be viewable on remote machines, configuration file located in the root directory of the current web application.
Left by juegos poker on 11/24/2009 10:42 AM
# re: Low impact Ajax with jQuery and ASP.NET MVC
Gravatar The ajax platform in java is too good to integrate many programs in this sort.
Regards,
Jookim....
Left by Free Photographs on 11/25/2009 6:54 AM
# re: Low impact Ajax with jQuery and ASP.NET MVC
Gravatar The drafting is the job we do and i want to know about the different software developed in java for the purpose....
Left by mechanical drafting on 11/25/2009 6:59 AM
# re: Low impact Ajax with jQuery and ASP.NET MVC
Gravatar I want to know about the ajax platform and its support to the jsp. As i have the sofware developed in it....
Regards,
Gater...
Left by roof truss drawings on 12/5/2009 11:53 PM
# wealthy affiliate review
Gravatar It's a good trick, I bookmarked up front.
Thanks for sharing !......Regards..Mr.Fuero..
Left by http://www.wealthyaffiliateresul on 12/7/2009 7:47 AM
# re: Low impact Ajax with jQuery and ASP.NET MVC
Gravatar The need of a good house for construction made me come here for you ti tell something about the designs we have here.
Regards,
Jack....
Left by San Diego MLS on 12/7/2009 11:24 PM
# re: Low impact Ajax with jQuery and ASP.NET MVC
Gravatar There is an housing insulation for the mortgages here and also the quality ones....
Left by Insulation Houston on 12/7/2009 11:30 PM
# re: Low impact Ajax with jQuery and ASP.NET MVC
Gravatar The ggcing collection agency offering lowest rate debt recovery services nationwide and internationally.
Left by debt recovery on 12/9/2009 6:29 AM
# re: Low impact Ajax with jQuery and ASP.NET MVC
Gravatar This is a vending machine mostly liked by all and you want to see i9t just follow me here..
Left by vending machines on 12/9/2009 6:32 AM
# re: Low impact Ajax with jQuery and ASP.NET MVC
Gravatar This is a natural pill mostly liked by all and you want to see it just follow me here. It will take a long drive in nights...
Left by Sperma on 12/9/2009 6:34 AM
# re: Low impact Ajax with jQuery and ASP.NET MVC
Gravatar Mouthwashes don’t always produce fresh breath: Some forms of halitosis need a stronger remedy.
Left by emergency dentistry on 12/9/2009 8:40 AM
# re: Low impact Ajax with jQuery and ASP.NET MVC
Gravatar if you want to detoxify your body completely, you would want to go in for natural cleansing methods.
Left by bowel cleanse on 12/10/2009 7:46 AM
# re: Low impact Ajax with jQuery and ASP.NET MVC
Gravatar For the development of companies Many companies rely solely on their website advertising to generate profits... imagine an ongoing campaign where you are guaranteed to reach your target market every day without the hefty ongoing expenses of radio
Left by Nepean Website Development on 12/13/2009 4:50 AM
# re: Low impact Ajax with jQuery and ASP.NET MVC
Gravatar To get a loan they need information when you register for a contest or survey or to receive a newsletter and all you can visit the web site for more details
Left by houston tx mortgage on 12/15/2009 4:23 AM
# re: Low impact Ajax with jQuery and ASP.NET MVC
Gravatar we provide innovative and effective CAD solutions to all structural drafting and design and structural engineering problems based on structural drafting and design documents.
Left by structural steel services on 12/17/2009 2:19 AM
# re: Low impact Ajax with jQuery and ASP.NET MVC
Gravatar Penis enlargement is combining penis exercises with topical DHT. Penis exercises increase both length and girth. Topical DHT increases penis tissue growth and healing.thanks for informative post.
Left by penisverlängerung on 12/17/2009 2:21 AM
# re: Low impact Ajax with jQuery and ASP.NET MVC
Gravatar That's good to know about this post where i like to discuss about this post with my friends thanks for sharing.
Regards,


Left by forex questions on 12/17/2009 11:41 PM
# re: Low impact Ajax with jQuery and ASP.NET MVC
Gravatar If you have all the time in the world to explore every single level, you definitely do not need the Horde Wow Guide
Left by horde guide on 12/18/2009 12:50 AM
# re: Low impact Ajax with jQuery and ASP.NET MVC
Gravatar Sydney Web in order to get the web presence they need to promote their services with the Aboriginal communities throughout Australia.
Left by Hills Website Design on 12/20/2009 2:06 AM
# re: Low impact Ajax with jQuery and ASP.NET MVC
Gravatar Every one should know how to Increases penis size volume 5 to 15% by increasing blood flow. If you're after "a little more size" and harder erections, penis pills are your answer.
Left by agrandir penis on 12/24/2009 6:20 AM
# re: Low impact Ajax with jQuery and ASP.NET MVC
Gravatar The Integrated Supply Chain and Operations Management major concentrates on strategic performance improvement of all business planning
Left by online bachelors degree on 12/26/2009 4:35 AM
# re: Low impact Ajax with jQuery and ASP.NET MVC
Gravatar The online merchants have attempted to address this concern by investing in and building strong consumer brands...
Left by wealthy affiliate results on 12/30/2009 1:21 AM
# re: Low impact Ajax with jQuery and ASP.NET MVC
Gravatar The results that we see on the FAP Turbo site show large profits, but of course they would. We want to discover whether real users earn with it.
Left by fap turbo on 1/2/2010 5:28 AM
# re: Low impact Ajax with jQuery and ASP.NET MVC
Gravatar I was reading something similar on another website that i was researching.
Started to thousands in delaware, maryland, dc, virginia, or west virginia.Getting in august before a scholar's productive home, the system is mentioned on loads at the university and meets in academic historical committees.We academic against our research like we massive against our online task?
http://www.DedreeHoyt.com
Left by Simi Valley Real Estate on 1/2/2010 10:52 PM
# re: Low impact Ajax with jQuery and ASP.NET MVC
Gravatar . Its all about chasing shadows.
By that I mean latching on to this or that latest, most innovative idea that some self styled money making guru has put out in the hope it’ll go viral and make them a lot of money off the backs of all the headless chickens who will follow them blindly down a blind alley. Its a shame but a truism nonetheless that people will follow where someone they see as an expert leads. Even if they lead them to certain disaster, which is what most of the gurus tend to do to their flocks.
The trick is to recognize a shadow when you see it!
www.onlineuniversalwork.com
Left by davidbaer on 2/6/2010 12:24 AM
# re: Low impact Ajax with jQuery and ASP.NET MVC
Gravatar I was reading something similar on another website that i was researching.
Dog Life Jacket
Left by Dog Life Jacket on 2/26/2010 9:07 AM
# re: Low impact Ajax with jQuery and ASP.NET MVC
Gravatar Is this a proven and tested program???
Left by ASX Options Trading on 3/10/2010 6:11 PM
# re: Low impact Ajax with jQuery and ASP.NET MVC
Gravatar I encountered this kind of program when I was still in college...
Left by Titleist Golf Balls on 3/12/2010 11:34 PM
# re: Low impact Ajax with jQuery and ASP.NET MVC
Gravatar Hi, Keep up the good work its nice to see a blog that stands out from the rest, this was a very informative read and as now been added to my favourites!!
Left by Diet Solution Program on 8/5/2010 12:17 AM
# Low impact Ajax with jQuery and ASP.NET MVC
Gravatar I think that the the topic Low impact Ajax with jQuery and ASP.NET MVC is really has so much knowledge about the ASP.NET MVC users I think that the Low impact Ajax with jQuery is one of the good things it has.
Left by College Research Papers on 11/2/2010 7:29 AM
# re: Low impact Ajax with jQuery and ASP.NET MVC
Gravatar thanks a lot! this is just what i've been looking for
Left by pre-ejaculation treatments on 11/12/2010 1:44 AM
# Low impact Ajax with jQuery and ASP.NET MVC
Gravatar The Low impact Ajax with jQuery and ASP.NET MVC is a think that has so much as an information for the peoples I think that it is really a useful one to have and learning about it by this information can make the work easier to do.
Left by College Research Papers on 11/13/2010 1:10 AM
# re: Low impact Ajax with jQuery and ASP.NET MVC
Gravatar I think that the given information about the Low impact Ajax with jQuery and ASP.NET MVC is one that has so much for learning I think that this information is to make good of this work and it will make the work more easier.I believe the information covered in the discussion is top notch. I've been doing a research on the subject and your blog just cleared up a lot of questions.
Left by download icons on 11/29/2010 4:54 AM
# re: Low impact Ajax with jQuery and ASP.NET MVC
Gravatar interesting news.
Left by Prestiti on 1/19/2011 4:44 AM
# re: Low impact Ajax with jQuery and ASP.NET MVC
Gravatar interesting news.
Left by Siti flash on 1/19/2011 4:46 AM
# re: Low impact Ajax with jQuery and ASP.NET MVC
Gravatar Can I just say what a aid to search out somebody who truly knows what theyre speaking about on the internet. You definitely know easy methods to deliver a problem to light and make it important. Extra folks must learn this and perceive this facet of the story. I cant imagine youre not more widespread since you definitely have the gift.

YOur articele helped me a lot as I am new with this topic especially ASP.NET
Left by Acne Free In 3 Days on 2/20/2011 12:22 PM
# re: Low impact Ajax with jQuery and ASP.NET MVC
Gravatar I have been searching for this quality blogs regarding this niche. Searching in Yahoo drove me here, I just found this kind of satisfactory readings i was looking for.Home renovations Costs I must bookmark this website to avoid missing it again.
Left by socialboom on 3/16/2011 3:04 PM
# re: Low impact Ajax with jQuery and ASP.NET MVC
Gravatar I think that the given information about the Low impact Ajax with jQuery and ASP.NET MVC is one that has so much for learning
Left by Prestiti on 3/25/2011 12:41 PM
# re: Low impact Ajax with jQuery and ASP.NET MVC
Gravatar I was reading something similar on another website that i was researching
Left by Mutui on 3/25/2011 12:42 PM
# re: Low impact Ajax with jQuery and ASP.NET MVC
Gravatar It's a good trick, I bookmarked up front.
Left by Giochi per Ragazze on 3/25/2011 12:44 PM
# re: Low impact Ajax with jQuery and ASP.NET MVC
Gravatar On second thoughts, I have trouble implementing this.
Left by Giochi Gratis on 3/25/2011 12:45 PM
# re: Low impact Ajax with jQuery and ASP.NET MVC
Gravatar Its nice to read some post about this nowadays. Its meaningful and somehow sharing this to readers like me, makes me want to surf the web to be able to get more wonderful ideas.Thank you for posting this. how to win the lottery

Left by marry on 3/28/2011 2:46 AM
# re: Low impact Ajax with jQuery and ASP.NET MVC
Gravatar A trader who will use forex trading robots should keep in mind that they have to get the best programmed robot to get high profits as possible. The system should fit their style of trading and be able to provide money returns as fast as possible. The best Forex Robot trading should also provide what the trader needs and wants in his or her trading.
Left by anokhi on 3/31/2011 5:15 AM
# re: Low impact Ajax with jQuery and ASP.NET MVC
Gravatar This is a very informative article.I was looking for these things and here I found it. I am doing a project and this information is very useful me. If you are interested, but this is my duty to inform you that virtual administrative assistant a very dedicated service and can be applied anywhere you want and get better results. How To Win The Lottery

Left by marry on 4/4/2011 6:05 AM

Leave Your Comment

Title*
Name*
Email (never displayed)
 (will show your gravatar)
Url
Comment*

 

Preview Your Comment.