In this article we will learn how to load the tags fromthe database in ASP.NET MVC Web API using AngularJS. Here we are going to use a pretty control called tagIt which does the tag part easier with a lot of configuration options. This article uses a normal MVC application which includes the Web API control in it; in addition to it, we use AngularJS for our client-side operations. Please see the links given below if you are new to these technologies.
As the article title implies, we are actually going to load the tags from a table called tblTags from SQL Server database. So now we will go and create our application. I hope you will like this.
You can always download the source code here: Load Tags From Database Using Angular JS In MVC Web API
Background
A few days back I was trying to use the tagIt widget in one of my applications. I could do that with some pre-defined tags as a variable. Then I thought why don’t we try something like loading these tags from the database? Hence my application uses MVC architecture; I selected Web API for retrieving the data fromthe database. I hope someone finds this useful.
Create a MVC application
Click File-> New-> Project then select MVC application. Before going to start the coding part, make sure that all the required extensions/references are installed. Below are the required things to start with.
- AngularJS
- TagIt Plugin
- jQuery
You can download all the items mentioned above from NuGet. Right click on your project name and select Manage NuGet packages.
Once you have installed those items, please make sure that all the items(jQuery, Angular JS files, Tag It JS files like tag-it.js) are loaded in your scripts folder.
Using the code
Before going to load the tags from a database we will try to load our tagit control with some predefined array values. No worries, later we will change this array values to the values we get from the database.
Include the references in your _Layout.cshtml.
As we have already installed all the packages we need, now we need to add the references, right?
- @RenderBody()
- @Scripts.Render("~/bundles/jquery")
- <script src="~/Scripts/jquery-2.2.0.js"></script>
- <script src="~/Content/jquery-ui.min.js"></script>
- <script src="~/Scripts/angular.js"></script>
- <script src="~/Scripts/angular-route.js"></script>
- <script src="~/Scripts/tag-it.min.js"></script>
- <script src="~/Scripts/MyScripts/TagScripts.js"></script>
- @RenderSection("scripts", required: false)
Here TagScripts.js is the JavaScript file where we are going to write our own scripts.
Set the changes in View
So we have added the references, now we will make the changes in our view. As we uses Angular JS, we will set our ng-app and ng-controller as follows.
- <div ng-app="tagApp">
- <div ng-controller="tagController">
- <ul id="tags"></ul>
- <div id="error">Nothing found!</div>
- </div>
- </div>
You can give a style as follows if you want.
- <style>
- #tags {
- width: 25%;
- }
- #error {
- display: none;
- font-weight: bold;
- color: #1c94c4;
- font-family: Trebuchet MS, Tahoma, Verdana, Arial, sans-serif;
- font-size: 1.1em;
- }
- </style>
Oops!, I forgot to mention, please do not forget to include the CSS style sheets.
- <link href="~/Content/jquery-ui.css" rel="stylesheet" />
- <link href="~/Content/jquery.tagit.css" rel="stylesheet" />
Now we can write the scripts in our TagScripts.js file.
Create Angular App
You can create an Angular module as follows.
- var app;
- //Angular App
- app = angular.module('tagApp', []);
Here the module name must be same as we have given in ng-app in our view.
Create Angular Controller
You can create an Angular Controller as follows.app.controller('tagController', function($scope)
- {
- $("#tags").tagit(
- {
- availableTags: availableTags,
- autocomplete:
- {
- delay: 0,
- minLength: 1
- },
- beforeTagAdded: function(event, ui)
- {
- if ($.inArray(ui.tagLabel, availableTags) < 0)
- {
- $('#error').show();
- return false;
- }
- else
- {
- $('#error').hide();
- }
- }
- });
- });
As you can see we have called tagit() function to initialize the tagit control. Below are the explanations to the options we have given.
- availableTags
availableTags are the source we need to apply to the control so that the given items can be shown on user actions.
- autocomplelte
autocomplete is a property which enables the auto complete option, when it is true user will get the items listed accordingly for each key press
- beforeTagAdded
beforeTagAdded is a callback function which gets fired before we add the new tags to the control. This function can be used to do all the needed tasks before adding the given item to the control. For example, if we need to load only the values from the database and if we need to restrict creating the new tags by the user, means user will be allowed to use only the available tags which is in the available tag array. The preceding code block does what has been explained above.
- if ($.inArray(ui.tagLabel, availableTags) < 0)
- {
- $('#error').show();
- return false;
- }
- else
- {
- $('#error').hide();
- }
If user tries to add a new tag, we will show the alert div which we already set in our view.
Now it is time to see the control in our view, let us see whether it works fine, if not, we may need to go back and check again.
It seems everything is fine, thank god we don’t need any debugging 
Now we will create a Web API controller. Oh yeah, we are going to start our real coding. Right click on your controller folder and click new.
So our Web API controller is ready, then it is time to go to do some AngularJS scripts, don’t worry we will come back here.
We need to make changes to our AngularJS controller tagController as follows.
- //Angular Controller
- app.controller('tagController', function($scope, tagService)
- {
- //Angular service call
- var tgs = tagService.getTags();
- if (tgs != undefined)
- {
- tgs.then(function(d)
- {
- availableTags = [];
- for (var i = 0; i < d.data.length; i++)
- {
- availableTags.push(d.data[i].tagName);
- }
- $("#tags").tagit(
- {
- availableTags: availableTags,
- autocomplete:
- {
- delay: 0,
- minLength: 1
- },
- beforeTagAdded: function(event, ui)
- {
- if ($.inArray(ui.tagLabel, availableTags) < 0)
- {
- $('#error').show();
- return false;
- }
- else
- {
- $('#error').hide();
- }
- }
- });
- console.log(JSON.stringify(availableTags));
- }, function(error)
- {
- console.log('Oops! Something went wrong while fetching the data.');
- });
- }
- });
As you have noticed, we are calling an AngularJS service here. Once we get the data from the service, we are assigning it to the array which we have initialized with the predefined values, so that the data from the database will be available in the tagit control. So can we create our AngularJS service?
- //Angular Service
- app.service('tagService', function($http)
- {
- //call the web api controller
- this.getTags = function()
- {
- return $http(
- {
- method: 'get',
- url: 'api/tag'
- });
- }
- });
This will fire our Web API controller and action GET api/tag. And the Web API controller will give you the results which we will format again and give to the available tag array. Sounds cool? Wait, we can do all these things without AngularJS, meaning we can simply call a jQuery Ajax Get. Do you want to see how? Here it is.







Ravi KandelPosted Jul 14, 2016, 11:38 AM
Nice share
Santhakumar MunuswamyPosted Feb 23, 2016, 2:18 PM
Thanks for nice article
Vignesh ManiPosted Feb 22, 2016, 5:50 PM
Nice one
Gowtham KPosted Feb 22, 2016, 11:39 AM
Good One
Sibeesh VenuPosted Feb 22, 2016, 1:00 AM
Shubham Kumar Thanks a lot
Sibeesh VenuPosted Feb 22, 2016, 1:00 AM
Humayun Kabir Mamun Thanks a lot
Sibeesh VenuPosted Feb 22, 2016, 12:59 AM
Saineshwar Bageri Thanks a lot
Sibeesh VenuPosted Feb 22, 2016, 12:59 AM
Gul Md Ershad Thanks a lot
Shubham KumarPosted Feb 22, 2016, 12:38 AM
nice one sir
Humayun Kabir MamunPosted Feb 21, 2016, 11:04 PM
Nice...
Saineshwar BageriPosted Feb 21, 2016, 11:02 PM
Nice one
Former memberPosted Feb 21, 2016, 9:31 PM
Good Article