2015-09-28

ASP.NET Web API application with Angularjs Forms and Bootstrap

Web Application development has reached new heights with its UI that is feature rich and responsive across multiple platforms. A blend of client-server side technologies allows us to create the next-gen apps on the web.



Angularjs, a relatively new and popular JavaScript framework can be used for DataBinding, Routing and Dependency Injection, whereas BootStrap.js, a widely used front-end framework can be used for styling and creating responsive, mobile-first web applications. ASP.NET MVC on the other hand is a nice server-side framework for developing web applications using the Model, Views and Controllers (MVC) pattern. One major release with the MVC (version 4 onwards) is ASP.NET WEB API. Web API’s allow us to create Http action based methods which can be called using JavaScript with Ajax (e.g. jQuery Ajax method and Angular.js $http methods).

In this article, we will be using Angular.js forms with Bootstrap for creating high responsive layout web applications with DataBinding. We will create UI form using custom Angular Directive and also will make use of the Angular UI Bootstrap for elements like DatePicker. ASP.NET Web API will be used to post the form data to the database.

Step 1:

Open the free Visual Studio 2013 Community Edition or any other VS 2013 edition with Update 4 installed. Create a new Empty MVC application with the name ‘A1_NG_Boot_MVC5’. In this project, in the App_Data folder, add a new Sql Server Database with the name ‘ApplicationEDMX.mdf’.

Step 2:

In the Models folder, add a new ADO.NET Entity Data Model of the name ApplicationEDMX. In the wizard, select Database First and the ApplicationEDMX.mdf as the name of the database. Select the PersonInformation table in the wizard.

Step 3:

In the Controllers folder, add a new WEB API Controller based on Entity Framework of the name PersonInformationAPICntroller. This will generate the necessary methods for performing Http Operations.

Step 4:

To use the Angular.js, Bootstrap and Angular UI Bootstrap in the current project, use NuGet Package Manager.

Step 5:

It’s time to add client-side logic in the project. In the project, add a new folder named ‘MyScripts’. In this folder add the following files:

pModule.js

The above module will be an entry point for using Angular logic on client side.

pService.js

The above service encapsulates calls to WEB API.

pController.js

The above controller contains logic for:

Submitting the form data to WEB API using submitForm() function.

Calculates Age of the person using calculateAge() function.

Sets the Current and Permanent address using SameAddress() function.

Reset all Scope objects using initialize() function. This function is called using cancelForm() function.

The controller defines arrays for Gender and Title of the registering person.

pTemplate.html

The above html file defines the form containing UI elements for accepting PersonInformation. The UI is designed using HTML and the following Bootstrap classes:

form-control

col-sm-3

col-sm-9

control-label

The form also contains Angular UI Bootstrap element type using <input type=’text’ for DatePicker:

The DatePicker will be displayed as a popup with the date format as dd/mm/yyyy.

Now we have to use this form to display UI in our Index page. To do so, we can use the Custom Directive creation feature of Angular.js. In the MyScripts folder, add the following JavaScript file of the name pDirective.js.

pDirective.js

The directive of name personForm is defined above. Angular will represent this as <person-form> in our view. The expression restrict: ‘E’ represents the directive to be used as Html element. The Html UI we created will be read using templateUrl property.

Step 6:

In the project, add a new Empty MVC Controller of the name PersonInfoMVC. Scaffold the Index View from the Index action method of the controller class.

Step 7:

In the Index.cshtml add the following html markup:

The above View contains the references for:

bootstrap.min.css – bootstrap framework for high responsive UI

angular.min.js – Angular Framework

Ui-bootstrap-tpls-min.js – Angular UI Bootstrap for DataPicker.

JavaScript files we have created in the previous steps.

The View has the HTML element <person-form> which we created in the previous steps with DataBinding.

Since we have used the Bootstrap, the page looks sleek and intuitive. The advantage of Bootstrap here is that if we resize the browser or browse the page in a Mobile/Tablet device, the UI will be automatically adjusted and displayed.

No #1 Recommended ASP.NET MVC Hosting

ASPHostPortal.com

ASPHostPortal.com  is the leading provider of Windows hosting and affordable ASP.NET MVC Hosting. ASPHostPortal proudly working to help grow the backbone of the Internet, the millions of individuals, families, micro-businesses, small business, and fledgling online businesses. ASPHostPortal has ability to support the latest Microsoft and ASP.NET technology, such as: WebMatrix, WebDeploy, Visual Studio 2015, .NET 5/ASP.NET 4.5.2, ASP.NET MVC 6.0/5.2, Silverlight 6 and Visual Studio Lightswitch, ASPHostPortal guarantees the highest quality product, top security, and unshakeable reliability, carefully chose high-quality servers, networking, and infrastructure equipment to ensure the utmost reliability.

HostForLIFE.eu

HostForLIFE.eu guarantees 99.9% uptime for their professional ASP.NET MVC hosting and actually implements the guarantee in practice. HostForLIFE.eu is the service are excellent and the features of the web hosting plan are even greater than many hosting. HostForLIFE.eu offer IT professionals more advanced features and the latest technology. Relibility, Stability and Performance of  servers remain and TOP priority. Even basic service plans are equipped with standard service level agreements for 99.99% uptime. Advanced options raise the bar to 99.99%. HostForLIFE.eu revolutionized hosting with Plesk Control Panel, a Web-based interface that provides customers with 24×7 access to their server and site configuration tools.

DiscountService.com.au

DiscountService.com.au is The Best and Cheap ASP.NET MVC Hosting. DiscountService.com.au was established to cater to an under served market in the hosting industry web hosting for customers who want excellent service. DiscountService.com.au guarantees the highest quality product, top security, and unshakeable reliability, carefully chose high-quality servers, networking, and infrastructure equipment to ensure the utmost reliability. DiscountService.com.au has ability to support the latest Microsoft and ASP.NET technology, such as: WebMatrix, WebDeploy, Visual Studio 2015, .NET 5/ASP.NET 4.5.2, ASP.NET MVC 6.0/5.2, Silverlight 6 and Visual Studio Lightswitch. DiscountService.com.au is devoted to offering the best Windows hosting solution for you.

Show more