AngularJS模型将NULL传递到MVC控制器



以下代码将null对象从AngularJS控制器发送到MVC控制器。在" batarang"中,$范围正在显示具有适当值填充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);
    }

员工类:

    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; }

帮助

这是因为您不是通过服务发布数据的原因,这就是为什么您在后端

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'}
    });
}

您可以从View

传递员工对象
  <form ng-submit="submitBasicInfo(EmployeeInfo)" name="EmployeeBasic" ng-controller="EmployeeBasicCtrl">

相关内容

  • 没有找到相关文章

最新更新