Click here to Skip to main content
15,916,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I write a web in mvc framework with angular js.

my application is:
JavaScript
var app = angular.module("AngularApp", []);


and my controller is:
JavaScript
app.controller("EmpCtrl", function ($scope, EmployeeService) {
GetAllEmployee();
function GetAllEmployee() {

    var getAllEmployee = EmployeeService.getEmployee();
    getAllEmployee.then(function (emp) {
        $scope.employees = emp.data;

    }, function () {
        alert('data not found');
    });
}


$scope.deleteEmployee = function (id) {
    var getData = EmployeeService.DeleteEmp(id);
    getData.then(function (msg) {
        GetAllEmployee();
        alert('Employee Deleted...');
        $scope.h1message = true;
        $scope.message = "ED";
    }, function () {
        $scope.h1message = true;
        $scope.message = "Error in Deleting Record";
    });
}
});

and my service is:
JavaScript
app.service("EmployeeService", function ($http) {
this.getEmployee = function () {
    debugger;
    return $http.get("/EmployeeModels/GetAllEmployee");
};

//Delete Employee
this.DeleteEmp = function (employeeId) {
    var response = $http({
        method: "post",
        url: "/EmployeeModels/deleteEmployee",
        params: {
            employeeId: JSON.stringify(employeeId)
        }
    });
    return response;
}

});


and my mvc action is :
C#
private ApplicationDbContext db = new ApplicationDbContext();

    public JsonResult GetAllEmployee()
    {
        using (ApplicationDbContext db = new ApplicationDbContext())
        {
            var employeeList = db.EmployeeModels.ToList();
            return Json(employeeList, JsonRequestBehavior.AllowGet);
        }
    }

    //DeleteEmployee
    public string DeleteEmployee(string employeeId)
    {
        if (employeeId != null)
        {
            int no = Convert.ToInt32(employeeId);
            var employeeList = db.EmployeeModels.Find(no);
            db.EmployeeModels.Remove(employeeList);
            db.SaveChanges();
            return "Employee Deleted";
        }
        else { return "Invalid Employee"; }
    }

and html file is:

<div ng-app="AngularApp" ng-init="name='hn';backGroundColor='red';
     person={firstname:'jo',lastname:'hary'}">
    <div ng-controller="EmpCtrl">
        <table border="1" width="100%">
            <tr>
                <th ng-click="orderByMe('EmployeeId')">employee id</th>
                <th ng-click="orderByMe('Address')">addres</th>
                <th ng-click="orderByMe('EmailId')">email id</th>
                <th ng-click="orderByMe('EmployeeName')">employee name</th>
            </tr>
            <tr ng-repeat="emp in employees|orderBy:orderByMe">
                <td> {{emp.EmployeeId}}</td>
                <td> {{emp.Address}}</td>
                <td>{{emp.EmailId}}</td>
                <td>{{emp.EmployeeName}}</td>
                <td>
                    <a data-ng-click="deleteEmployee(emp.EmployeeId)" style="cursor:pointer;">Delete</a>|
                    
                </td>
            </tr>
        </table>
        <br />
 </div>
</div>



the view of data is ok. and I call GetAllEmployee() in delete function. but when I delete record to table of database, view not refresh data?

What I have tried:

not refreshing data when a record is delete.
Posted
Updated 18-Feb-17 6:48am
v4
Comments
pankaj287 18-Feb-17 12:42pm    
Could you please confirm that `GetAllEmployee` method is getting called from your delete method success??

// Find index of the user
var index = $scope.users.indexOf(id);
// Remove user from array
$scope.users.splice(index, 1);




$scope.DeleteUser = function (id) {
    $http.delete('/api/users/' + id).success(function (data) {
        var index = $scope.users.indexOf(id);
        $scope.users.splice(index, 1);
    });
}
 
Share this answer
 
Comments
Member 12997509 13-Feb-17 5:47am    
data in table of sql and delete with mvc action. When a record delete, data in view not refresh.
I suspect this could be because of EF caching. I'd highly recommend to turn it off and then try the same. For the same you could use `AsNoTracking` while retrieving the records from `DBContext`.

public JsonResult GetAllEmployee() {
    using (ApplicationDbContext db = new ApplicationDbContext())
    {
            var employeeList = db.EmployeeModels.AsNoTracking().ToList();
            return Json(employeeList, JsonRequestBehavior.AllowGet);
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900