asp.net-mvc - Angular JS Put 方法与 ASP.NET MVC

标签 asp.net-mvc angularjs angularjs-resource

我为 Put 和 Get 方法定义了这样的 Angular Controller 。我是 Angular JS 的新手。 get 方法按预期工作,但 Put 方法没有被调用。我按照其中一个教程进行操作并实现了。尽管在教程中,他们使用 REST 服务 URL 而不是 Controller 方法。我在这里使用 MVC Controller 而不是 webAPI。

public JsonResult GetEmployee(int id)
        {
            Employee empDetail = emp.GetEmployees().FirstOrDefault(i => i.EmpID == id);
            return Json(empDetail, JsonRequestBehavior.AllowGet);
        }


        public JsonResult PutEmployee(int id, Employee updtEmp)
        {
            updtEmp.EmpID=id;
            int index = emp.GetEmployees().FindIndex(i => i.EmpID == updtEmp.EmpID);
            emp.GetEmployees().RemoveAt(index);
            emp.GetEmployees().Add(updtEmp);
            return Json(emp.GetEmployees(), JsonRequestBehavior.AllowGet);
        }

这是我的 angular.factory 和 Controller 方法

myservice.factory('empFactory', function ($resource) {

    return $resource('../../Employee/GetEmployee/:EmpID', { EmpID: '@EmpID' },
        {
            show: { method: 'GET' },
            update: { method: 'PUT', params: {  Employee: '@employee' } }
        });
});

myApp.controller('empDetailController', function ($scope, empFactory, $routeParams) {

    $scope.Employee = empFactory.show({ EmpID: $routeParams.EmpID });

    $scope.UpdateEmp = function () {
       // alert($scope.Employee.FirstName);
        var employee=$scope.Employee;
        empFactory.update({ EmpID: $routeParams.EmpID, Employee: employee })
    };
});

最佳答案

您在 MVC Controller 中的何处提到该方法是 PUT,直到并且除非您提到方法类型 HttpPut 或 HttpPost 等,否则它们将被视为 HttpGet,在 MVC Controller 中将该方法称为 PUT 方法,然后重试。

像这样装饰你的 MVC PUT 方法:

[HttpPut]
public JsonResult PutEmployee(int id, Employee updtEmp)
    {
        updtEmp.EmpID=id;
        int index = emp.GetEmployees().FindIndex(i => i.EmpID == updtEmp.EmpID);
        emp.GetEmployees().RemoveAt(index);
        emp.GetEmployees().Add(updtEmp);
        return Json(emp.GetEmployees(), JsonRequestBehavior.AllowGet);
    }

工厂和 Controller 应该是这样的:

myservice.factory('empFactory', function ($resource) {
var resource = {
employees:$resource('../../Employee/GetEmployee/:EmpID', { EmpID: '@EmpID' }),
empUpdateService:$resource('../../Employee/PutEmployee/:EmpID', { EmpID: '@EmpID' },{            
        update: { method: 'PUT', params: {  Employee: '@employee' } }
    })
};

 return resource;
});

myApp.controller('empDetailController', function ($scope, empFactory, $routeParams) {

$scope.Employee = empFactory.employees.get({ EmpID: $routeParams.EmpID });

$scope.UpdateEmp = function () {
   // alert($scope.Employee.FirstName);
    var employee=$scope.Employee;
    empFactory.empUpdateService.update({ EmpID: $routeParams.EmpID, Employee: employee })
};

关于asp.net-mvc - Angular JS Put 方法与 ASP.NET MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30309264/

相关文章:

javascript - Sigma.js 过滤器问题

angularjs - Angularjs 的事件记录或数据映射器模式?

angularjs - 为多个API调用构建 '$resource'服务

javascript - 响应类型为 text/plain 的 Angular 资源总是生成一个字符串数组

asp.net-mvc - Application_AuthenticateRequest 上的 ASP.NET MVC 重定向困境

c# - 我怎样才能遍历所有路线?

javascript - 在多个 $http 调用上显示微调器

javascript - AngularJS 单元测试时无法加载模块

c# - 如何在 MVC 中运行控制台应用程序

asp.net-mvc - asp.net mvc 标签 : <%: %> vs. <%= %>