« Share your Visual Studio code examples on your blog | Main | Linq to SQL delete multiple/bulk records (batch delete) »
Wednesday
Aug262009

jQuery Tag Suggestion in ASP.NET MVC

On a ASP.NET MVC project I am currently working on I have implemented Remy's jQuery Tag Suggestion
plug in which works perfectly and makes a nice experience for people using my application.

I wanted to share a couple of learnings for others who would like to implement this plugin in their ASP.NET MVC applications where they need to return data from a database instead of a static tag list/file.

To make it easier for you to implement a similar function I have created an ASP.NET MVC project which can be downloaded here Tag Suggestion ASP.NET MVC project files and Tag Suggestion database back up. You can also check out a working version of this source code here ASP.NET Tag Suggestion demo

Of course the first step is to download the tag.js file from Remy's article and add this file to your Scripts folder in your MVC project.

Then you will need to add a reference to this file and add the tag script under the Head html tag in your Site.Master file normally found under the Views/Shared folder.

The reference to the tag.js file will look something like

   12     <script src="<%= Url.Content("~/Scripts/jquery-1.3.2.min.js") %>" type="text/javascript"></script>

   13

   14     <script src="<%= Url.Content("~/Scripts/tag.js") %>" type="text/javascript"></script>

   15

   16     <% if (false)

   17        { %>

   18

   19     <script src="../../Scripts/jquery-1.3.2.min.js" type="text/javascript" </script>

   20

   21     <script src="../../Scripts/tag.js" type="text/javascript"></script>

   22

   23     <% } %>

 

and the tag script for the Ajax version of the Tag Suggestion which will look up and return data from your database. The script should look like

   25     <script type="text/javascript">

   26

   27         $(function() {

   28             $('#ParentTags').tagSuggest({

   29                 url: 'TagSuggestion',

   30                 delay: 250

   31             });

   32         });

   33

   34     </script>

 

Notice the #ParentTags which will be the id of the textbox you would like to use for the Tag Suggestion feature. This textbox will call/fire the jQuery function. Also notice the url: 'TagSuggestion' which points to a action within the current controller file being used to return the view where you will be using this tag suggestion textbox.

Now you need to add the textbox to the view where you want to use the Tag Suggestion like this

   19 <%= Html.TextBox("ParentTags")%>

 

As you can see this text box has a name/id of "ParentTags" which will link to the script also called #ParentTags. To make it easy and clean I have also got a field in my database called "ParentTags" so I can save the tags the person enters.

Last but not least we need to add the Action to the Controller which will be used to render the view we added the textbox to above.

The Action code will look like

   82         public ActionResult TagSuggestion()

   83         {

   84

   85             var tagDetailsGrp = from tdg in db.vw_TagDetails_Grps

   86                                 orderby tdg.DetailIDCount descending

   87                                 select tdg;

   88

   89             ArrayList list = new ArrayList();

   90

   91             foreach (vw_TagDetails_Grp item in tagDetailsGrp)

   92             {

   93                 list.Add(item.DetailTag);

   94             }

   95

   96             return this.Json(list);

   97

   98         }

 

This Action code goes to a view in my database which groups all of the tags from the table called TagDetails and returns this list of tags. The code then adds each tag to an ArrayList which is then returned to the jQuery function in JSON format.

That's it now you should have your new jQuery Tag Suggestion function up and running. I hope this helps to save you a bit of time and feel free to let me know if you have any questions.

PrintView Printer Friendly Version

EmailEmail Article to Friend

References (3)

References allow you to track sources for this article, as well as articles that were written in response to this article.

Reader Comments (13)

very nice

April 16, 2010 | Unregistered Commentermanu

ASP.NET MVC is a part of the ASP.NET Web application framework. It is one of the two different programming models you can use to create ASP.NET Web applications,the other being ASP.NET Web Forms @ http://www.utsin.com/

Yet another popular ugg boots
signature style is the Wilshire Signature logo boots. The lining of course is sheepskin (a material used by ugg
for majority of their styles) and this signature style comes with a 15-inch trimmed collar. uggs
are a popular choice when buying winter boots for children. Kids’ signature styles offer many options. A favorite is the UGG Australia
signature look, the classic boots. Another signature style ugg bailey button
for kids is the Kids Uptown. This style of shoe is designed for ugg bailey button boots
wear, and the laces are made of leather.

August 12, 2010 | Unregistered Commenterugg

discount gucci handbags, gucci outlet, wholesale handbags, gucci purses, gucci sunglass, coach outlet store online, handbags coach, cheap coach bags, cheap coach purses, coach bags on sale, fendi spy bag, cheap fendi handbags, coach handbags on sale, cheap designer sunglasses, coach wallets, hermes bags, hermes handbag, hermes birkin bags, burberry uk, burberry bags, burberry handbags, burberry scarf, chanel replica, replica chanel, replica chanel handbags, chanel uk, replica chanel handbags, louis vuitton sale, lv bags, lv handbag, louis vuitton wallets, louis vuitton replica, louis vuitton uk, yves saint laurent shoes, cheap sunglasses, ray ban wayfarers, ray ban aviator sunglasses, wholesale sunglasses, tiffany jewelry, tiffany and co, tiffany & co, tiffany rings, tiffany sale, cheap mac cosmetics, mac cosmetics, mac uk, cheap mac, balenciaga bags, prada bags, air max 90, 95 air max, air max 2009, air max 1, nike shox nz, cheap nike shox, shox shoes, air yeezy shoes, adidas mens shoes, adidas shoes, dc men's shoes, shoes gucci, gucci shoes men, gucci sneakers, lacoste shoes men, puma women's shoes, mbt men's shoes, prada shoes for men, timberland men's shoes, ed hardy women's shoes, tiffany bracelet, supra shoe, dolce gabbana shoes, christian audigier clothes, mbt shoes women, new balance outlet, adidas track jacket, affliction clothing, affliction t shirts, cheap ed hardy, ralph lauren shirts, polo ralph lauren shirts, a&f, abercrombie t shirt, abercrombie online store, lacoste polo shirt, manolo blahnik online store, giuseppe zanotti, alexander mc queen, Gianmarco Lorenzi, five fingers shoes, louboutin shoes, louboutin boots, christian louboutin, air force 1 shoes, nike uk, nike discount, nike stores, sneakers nike, cheap air jordans, nike sb dunk, cheap nfl jerseys, nhl jerseys, cheap ed hardy, ed hardy womens, ed hardy uk, cheap ed hardy, ed hardy shirts, p90x dvds, p90x workout schedule, buy p90x, p90x for sale, spyder ski jacket, canada goose jackets, north face jackets on sale, the north face outlet, jimmy choos, jimmy shoes, armani uk, hugo boss outlet, calvin klein uk, jean diesel, dsquared2 jeans, jack & jones jeans, levi jeans, jean lee, cheap ed hardy, true religion jeans on sale, classic short ugg, ugg sale, ugg australia, cheap ugg boots, cheap ugg, ugg boots cheap, cardy ugg boots, genuine ugg boots, ugg bailey boots, ugg store, black uggs, ugg boots for women, classic ugg boots, classic tall ugg boots, ugg classic short boots, ugg classic cardy boots, discount uggs boots, ugg on sale, chestnut uggs, ghd pure, white ghd, ghd iv, ghd uk, pink ghd straighteners, planchas de pelo ghd, ghd mk4, ghd hair, best hair straightener, ghd hair straighteners, purple ghd straighteners, pump shoes, mens jeans cheap, mens winter jackets, replica handbags, wholesale bags, cheap purses, designer handbag, ghd mini, best hair straighteners, short uggs, ugg boots uk, discount uggs, girls ugg boots, ugg tall, ralph lauren shirt, ralph lauren t shirt, abercrombie shirt, manolo blahniks, christian louboutins, mlb jerseys, ed hardy shirt, hugo boss uk, levis uk, adidas womens shoes, shoes puma, supra men's shoes, handbags gucci, coach outlet stores, coach handbags outlet, lv bag, lv purse, rayban sunglass, discount sunglasses, mac cosmetic, curse wow addons, addon wow, addons wow, blu ray player software, christian louboutin heels, replica christian louboutin, cheap christian louboutin, christian louboutin discount, Christian Louboutin sandals, mbt sale, womens mbt shoes, mbt shoes men, mbt shoes discount

August 12, 2010 | Unregistered Commenterair-max

What comes first is the fine quality of the ugg boots . On one hand, the ugg stand wear and tear and on the other hand, the uggs with warm wool inside are really good at keeping cold away from our feet. Maybe the two practical functions of the brand were the original pursuits of the ugg sundance , which still exist as one of the very important reasons for the popularity of the ugg sundance boots .

September 24, 2010 | Unregistered Commenterswf

Sounds great but does it work?

I try the code at , but i got an error.

August 4, 2011 | Unregistered CommenterCasinoGambler

In this modern and fashionable society, people are pursuing for ugg australia boots cool, unique, stylish and innovative. Whether it is uggs australia uk or fashion accessories all means a lot for modern society of today. Same is the case with trendy looking australia ugg boots. When these are cheap Ugg Boots, the excitement just gets doubled. Most chic looking UGG Classic Cardy Boots are in fashion now. These are one of the favorite fashion accessories for men and women long time ago. If you have not yet tried UGG Ultra Tall Boots, it's time to own one and feel the difference it can make to your personality. These are just brilliant and fabulous uk ugg boots sale. They are most iconic and can provide you with a new feeling and enhance confidence. The quality of discount uggs is just superior to what you have dreamt of. Today owning a new and trendy looking ugg boots sale are not only meant for the wealthy people. These are now made luxurious and affordable Cheap ugg Boots to reach out to every budget and range. You can just enjoy them by ordering cheap Ugg Boots online where you get the complete satisfaction and genuine quality at best possible rates. The finish, quality and designs you get from ugg boot sale are really astonishing and you will love them all. Different styles and designs of uk ugg boots sale are now available to make your wrist beautiful. You can choose from the wide variety of ugg boots on sale by comparing lots of perfect and stunning pieces. These UGG Ultra Short Boots would be nice investments for you in long term. These UGG Classic Mini Boots are brilliant and prove to be wonderful for you while it makes you stand out of the crowd. The dashing and extraordinary cheap boots you get will attract every eye in a crowd and makes you feel more confident. If you are not having ugg boots cheap yet, check them out and own them now as were not so affordable ever before. You will not miss them out. Be it Christmas or Valentine's Day or any other special day that you would like to celebrate, a Ugg Boots sale gift would be just perfect for any occasion. Many people think of the perfect discount uggs to purchase for their loved ones. Even if you shop for your special occasion at the last moment, you are sure to find something nice at ugg nightfall boots. UGG Sundance II Boots gifts are not restricted to just your lover, you could also pick perfect for almost everyone, including your family and friends.

November 30, 2011 | Unregistered CommenterUgg Boots sale

<p>Helplessly watched the light on his brow, Mu Chen is unable to escape, when white light from him and a scale far when, he saw, flying is augg boots sale previous hit him in the shoulder in the heart of the stone as like as two peas. Iugg boots clearancen the face of the flying stones, he helpless eyes closed.</p>

December 6, 2011 | Unregistered Commenterwendy

best anti wrinkle cream for face
Anti Wrinkles Creams & best anti wrinkle cream


It's nonetheless extremely important to make correct number of skin- care products. Here's a detailed review of the solution. on. Vitamin C+ Moisturize + Sunscreen Comply with these simple steps twice a day and your skin will look and feel softer and smoother. It is estimated that by the yr 2030, that number will enhance the place can i buy hydroxatone to 47 million women per year. Men' s skin is extra susceptible to irritation than ladies' s, attributable to constant where can i buy hydroxatone shaving.

thanks for sharing

December 30, 2011 | Unregistered CommenterTory Burch Outlet

With thunder two years and $6.5 million contract renewal

8. With thunder two years and $6.5 million contract renewal of KuiAn-cook. Cook in 2007 the first round of the 76 draft night 21 people team selected,cheap nike air max,
cheap nike air max 24/7,
nike shoes outlet,
cheap nike air max 90,
cheap nike air max 2012,
cheap nike air max shoes the four seasons has played in the heat and thunder. In the 222 games, cook averaging can get 7.5 points and 2.3 rebounds. On behalf of his team last season thunder 43 games,Wholesale nba Jerseys,cheap nhl Jerseys,Wholesale NFL Jerseys , NBA Jerseys,cheap mlb jerseys, NFL Throwback Jerseys,cheap nfl Jerseys,cheap NBA Jerseys cook all present 13.9 minutes with the can get 5.6 points and 1.7 rebounds, his 3-point shot 42.2%. And the three points is the lack of the thunder,cheap air max,
cheap air max shoes,
cheap jordan shoes,
cheap air max 90,
nike air max 2012,
air max shoes this is also out of contract he thunder decided to reason.cheap nfl jerseys,
cheap nhl jerseys,
football jerseys,
nfl football jerseys,
cheap nfl football jerseys,
cheap mlb jerseys,
cheap nba jerseys,
nfl jerseys cheap,
wholesale nfl jerseys
9.76 people team this summer signing first cheap nba shoes rookie nicola-wu's cut and second round picks pull walter Iraq Allen. Wu's cut from nba basketball jerseys,
cheap nba basketball shoes,
nba jerseys,
cheap nba shoes,
cheap NBA Throwback Jerseys,cheap nba jerseys the university of southern California, he in the first round of the 16 th overall 76 team selected, and walter Iraq is in total 50 overall pick.Green Bay Packers NFL Jerseys,
San Francisco 49ers NFL Jerseys,
Baltimore Ravens NFL Jerseys,
New Orleans Saints NFL Jerseys,
Pittsburgh Steelers NFL Jerseys,
New York Giants NFL Jerseys,
New England Patriots NFL Jerseys,
New York Jets NFL Jerseys,Cheap nfl jerseys,
nfl jerseys,
cheap mlb jerseys,nfl jerseys from china,cheap nhl jerseys,wholesale nfl jerseys,nfl jerseys cheap In addition, 76 people and free player Antonio Anderson, dwyane-Jones,wholesale authentic jerseys,
cheap authentic jerseys,cheap nhl jerseys,
wholesale authentic nfl jerseys,cheap mlb jerseys,
cheap authentic nfl jerseys.
Green Bay Packers NFL Jerseys
San Francisco 49ers NFL Jerseys
Baltimore Ravens NFL Jerseys
New Orleans Saints NFL Jerseys
Pittsburgh Steelers NFL Jerseys
New York Giants NFL Jerseys
New England Patriots NFL Jerseys mike-of Atlantis, and Xavier-sheila, so that 76 people of the book's list to reach 15 people.Herve Leger,
Herve Leger on sale,
authentic Herve Leger,
wholesale Herve Leger,
cheap Herve Leger,
Herve Leger Dress

January 10, 2012 | Unregistered Commenternflstorechina

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>