Tuesday, 23 June 2015

Filter table in angular js with example in MVC


  1.Open visual studio Click on new project select Asp.net MVC web application as shown below
                           


2.then add controller give  name as Home

3.write  JsonResult method in Home controller as shown below.

       public JsonResult GetPersons()
        {
            angularDBEntities e = new angularDBEntities();
            var result = e.EmpTBs.ToList();
            
            return Json(result, JsonRequestBehavior.AllowGet);

        }

GetPersons() returns employee list,here we used entityframework to interact with database.
EmpTB is the table name 


4.add one js file give name as Employee.js


-->create module in angular js


var EmpApp = angular.module('EmpApp', [])--


--> create service/factory to get employee list from server

EmpApp.factory('EmpService', ['$http', function ($http) { 
    var EmpService = {};
    EmpService.getEmps = function () {
        return $http.get('/Home/GetPersons');
    };
    return EmpService;

}]);
here,$http.get() calls controller method getpersons()

In AngularJS world, the services are singleton objects or functions that carry out specific tasks. It holds some business logic. Separation of concern is at the heart while designing an AngularJS application. Your controller must be responsible for binding model data to views using $scope. It does not contain logic to fetch the data or manipulating it.

AngularJS can manage these service objects. Wherever we want to use the service, we just have to specify its name and AngularJS auto-magically inject these objects 

We saw there are two ways of defining an angularjs service. Using module.factory and module.service.

module.service( 'serviceName', function );
module.factory( 'factoryName', function );



When declaring serviceName as an injectable argument you will be provided with an instance of the function. In other words new FunctionYouPassedToService(). This object instance becomes the service object that AngularJS registers and injects later to other services / controllers if required.

When declaring factoryName as an injectable argument you will be provided with the value that is returned by invoking the function reference passed to module.factory.


---> create angular js controller with in this scope call service bind employee list to Emps

EmpApp.controller('EmpController', function ($scope, EmpService) {
    $scope.Emps = [];
    getEmps();
    function getEmps() {
        EmpService.getEmps()
            .success(function (emps) {
                $scope.Emps = emps;
                console.log($scope.Emps);
            })
            .error(function (error) {
                $scope.status = 'Unable to load customer data: ' + error.message;
                console.log($scope.status);
            });
    }
});


MVC for beginners

ASP.NET MVC Overview:

The Model-View-Controller (MVC) architectural pattern separates an application into three main components: the model, the view, and the controller.

Model

- Responsible for storing and retrieving data
- Maintenance of states.
- Notification of observers of change in state.
- Classes or references including .cs and .dll files

View

- Responsible for rendering UI model.
- User interaction (Text Box, List Box, Button, Label)
- Normally hold .cshtml, .aspx pages to show output.

Controller

- Responsible for responding to user input.
- Instructing model to process the user input.
- Make a bridge between view and model.


Every controller holds one or more than one action method(s). These action methods are calling during the execution through URL like let’s say the URL is localhost/Home/Index.

Here Home is the controller name and Index is the ActionMethod name. Now every Action method has its own View by its name. So here a view will be stored within the View à Home folder namedIndex.cshtml.

Whenever you will request for Home/Index the index.cshtml file will be shown within that View àHome folder


Now let’s see how to start this. Open your Visual Studio 2013 Choose project and set your name like as shown below



Now open your solution explorer in that we have three folders named Model, Controller and View and it will contain two web.config in solution one in the root folder and another one in the View folder.

Normally Home is the default Controller and Index is the default action method. If you open theRouteConfig.cs file in the App_Start folder you will come to see in the method RegisterRoutes that is default controller and ActionMethod is Home and Index respectively. If you want to change it you can do easily.

OK now let’s start with a simple program. Right click on the Controller folder and choose Create new Controller. Name it HomeController.





Now run your project and run with localhost/Home/Index and see whats the output. It will show the data of Index.cshtml within Home folder.