خرید بک لینک

Vote count: 0

I am trying to develop a simple form using angular js 1.* and asp.net mvc 5. When I try to save the form data using $http(), it hits the action but none of the data is passed. But if I use $.post(), it works perfectly.

Could anybody please explain me why? Is that because we need to use ApiController instead of normal mvc's Controller? Or what could be the potential problem?

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",
        });

        retu request;
    }

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

        retu request;
    }

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

    //    retu request;
    //}




    //for creating a new employee
    **this.post = function (emp) {
       // retu $.post('/Employee/Save', emp); //this one works
        retu $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 }
        });

        retu request;
    }       

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

        retu request;
    }
}) })();

EmployeeController

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

        retu Json(db.SaveChanges());
    }

asked 1 min ago

برچسب: نویسنده: استخدام کار تاريخ: پنجشنبه 13 خرداد 1395 ساعت: 14:51

صفحه بندی