angular js,asp.net mvc:$ http是否仅适用于ApiController而不适用于普通的Controller?

时间:2016-06-02 07:37:03

标签: angularjs asp.net-mvc-5

我正在尝试使用angular js 1. *和asp.net mvc 5开发一个简单的表单。当我尝试使用$ http()保存表单数据时,它会点击操作但没有传递任何数据。但是,如果我使用$ .post(),它的效果非常好。

有人可以解释一下为什么吗?那是因为我们需要使用ApiController而不是普通的mvc控制器吗?或者可能是什么潜在问题?

AddEmployeeController.js

(function () {
'use strict';

angular.module('ApplicationModule').controller('AddEmployeeController', function ($scope, SPACRUDService) {
    $scope.Id = 0;

    $scope.employee = {
        FirstName: 'Hello',
        MiddleName: '',
        LastName: '',
        CurrentAddress: '',
        PermanentAddress: '',
        Gender: 'M',
        MobilePhone: '',
        HomePhone: '',
        Email: '',
        CitizenshipNumber: '',
        FatherName: 'n/a',
        DOB: '',
        TaxID: '',
        EmergencyFullName: '',
        EmergencyRelationship: '',
        EmergencyPhoneNo: '',
        EmergencyMobileNo: '',
        IsCitizenshipCertProvided: false,
        IsAcademicCertProvided: false,
        IsExpOrReferenceLetterProvided: false
    };

    $scope.save = function () {

        var Employee = $scope.employee;

        var promisePost = SPACRUDService.post(Employee);

        promisePost.then(function (data) {
            alert("Saved Sucessfully!");
        },
            function (error) {
                $scope.error = "Failed ", error;
            }
        );
    };
});

AddEmployeeController.$inject = ['$location']; 

function AddEmployeeController($location) {
    /* jshint validthis:true */
    var vm = this;
    vm.title = 'AddEmployeeController';

    activate();

    function activate() { }
} })();

Service.js

(function () {
'use strict';

angular.module('ApplicationModule').service("SPACRUDService", function ($http) {

    //gets all employees 
    this.getEmployees = function () {
        var request = $http({
            method: "get",
            url: "/Employee/GetAllEmployees",
        });
        return request;
    }

    //gets a employee by id 
    this.getEmployee = function (Id) {
        var request = $http({
            method: "get",
            url: "/Employee/GetEmployeeById",
            data: id
        });

        return request;
    }

    //creates a new employee 
    //this.post = function (Employee) {
    //    var request = $http({
    //        method: "post",
    //        url: "/Employee/Save",
    //        data: Employee 
    //    });
    //    return request;
    //}

    //for creating a new employee
    this.post = function (emp) {
       // return $.post('/Employee/Save', emp); //this one works
        return $http.post('/Employee/Save', emp); //this doesn't
    }

    //updates the chosen employee
    this.put = function (Employee, Id) {
        var request = $http({
            method: "post",
            url: "/Employee/Update",
            data: { id: Id, employee: Employee }
        });
        return request;
    }       

    //removes the employee
    this.put = function (Id) {
        var request = $http({
            method: "post",
            url: "/Employee/Delete",
            data: id
        });

        return request;
    }
}) })();

EmployeeController

    [HttpPost] 
    public JsonResult Save(Employee emp)
    {
        db.Add(emp);

        return Json(db.SaveChanges());
    }

1 个答案:

答案 0 :(得分:0)

新的.net核心mvc要求[FormBody]附加到参数(如果它作为Json Data出现)。此行为来自web api 2.

[HttpPost] 
public JsonResult Save([FormBody]Employee emp)
{
    db.Add(emp);
    return Json(db.SaveChanges());
}