Angularjs Model将null传递给MVC控制器

时间:2016-12-23 06:28:30

标签: angularjs model-view-controller model null

以下代码从Angularjs控制器向MVC控制器发送空对象。在“batarang”中,$ scope显示EmployeeInfo对象,其中正确的值填写HTML表单。但是MVC方法使所有值都为空。我的代码如下

控制器:

Angular.module("myApp", []).controller("EmployeeBasicCtrl", function ($scope, $http) {
$scope.signBox = false;

$scope.SEX = [
    { text: "Non of above", value: "N" },
    { text: "Female", value: "F" },
    { text: "Male", value: "M" },

]
$scope.submitBasicInfo = function () {

    $http({
        method: 'POST',
        url: '/EmployeeInfo/AddEmployee'
    }).success(
    function (resp) {
        $scope.success = resp.success;
        $scope.Message = resp.Message;
    }
)// success en

    } // end of submit form
})// end of controller

HTML:

    <form ng-submit="submitBasicInfo()" name="EmployeeBasic" ng-controller="EmployeeBasicCtrl">

    <div class="well">
        <input type="hidden" name="EmpID" />
        <div class="panel panel-heading panel-primary">
            <div class="panel-heading"><h3> Personal Information </h3></div>
            <label> First Name </label>
            <input name="Fname" class="form-control" ng-model="EmployeeInfo.Fname" required />
            <small ng-show="EmployeeBasic.Fname.$touched && EmployeeBasicEmployeeBasic.Fname.$invalid">First Name is mandatory</small><br />
..........

MVC控制器:

[HttpPost]
    public JsonResult AddEmployee(EmployeeInfo para)
    {
        return Json(new {success="success" },JsonRequestBehavior.AllowGet);
    }

EmployeeInfo类:

    public class EmployeeInfo
{
    public string EmpID { get; set; }
    public string Fname { get; set; }
    public string Sname { get; set; }
    public DateTime DOB { get; set; }
    public string Email { get; set; }
    public string Mobile { get; set; }
    public string Gender { get; set; }
    public string UnitNo { get; set; }
    public string StreetNo { get; set; }
    public string Suburb { get; set; }
    public string StateID { get; set; }
    public string PostCode { get; set; }

感谢帮助

1 个答案:

答案 0 :(得分:0)

这是因为您没有通过服务发布数据,这就是您在后端获得null的原因

你的帖子请求应该是

 $scope.EmployeeInfo ={'Fname':'',EmpID:''....all your child obj of EmployeeInfo }

$scope.submitBasicInfo = function (EmployeeInfo) {

    $http({
        method: 'POST',
        url: '/EmployeeInfo/AddEmployee',
        data: EmployeeInfo, // what data you want to send 
        headers: {'Content-Type': 'application/json'}
    });
}

您可以从视图中传递EmployeeInfo对象

  <form ng-submit="submitBasicInfo(EmployeeInfo)" name="EmployeeBasic" ng-controller="EmployeeBasicCtrl">