将AngularJS模型发布到ASP.NET MVC控制器中的复杂模型

时间:2015-05-26 15:42:13

标签: javascript asp.net-mvc angularjs

我正在尝试使用JavaScript创建模型,并通过$ http.post将其发布到ASP.NET MVC控制器方法。检查发布的JSON时,将填充Address属性。但是,当命中ASP.NET MVC控制器时,所有相应的Address属性都为null。

角度控制器方法:

$scope.submitForm = function(isValid) {
    alert("Form is valid!");

    var member = {
        TempCardNumber: $scope.tcardno,
        Title: $scope.title,
        FirstName: $scope.firstName,
        LastName: $scope.lastName,
        Address: [
            {
                AddressLine1: $scope.address1,
                AddressLine2: $scope.address2,
                AddressLine3: $scope.address3,
                AddressLine4: $scope.address4,
                TownCity: $scope.towncity,
                County: $scope.county,
                PostCode: $scope.postcode,
                PhoneNumber: $scope.phone
            }
        ],
        EmailAddress: $scope.email
    };

    $http({
        method: 'POST',
        url: '/Home/Index',
        data: member
    }).success(function (data, status, headers, config) {

    });
}

发表JSON:

{"TempCardNumber":"633341677489986320","Title":"Mr","FirstName":"test","LastName":"user","Address":[{"AddressLine1":"8 Eaton Close","AddressLine2":" Hatton","TownCity":" Derby","County":" Derbyshire","PostCode":" DE65 5ED","PhoneNumber":"01234567890"}],"EmailAddress":"test@gmail.com"}

ASP.NET MVC控制器:

public ActionResult Index([FromBody]MemberModel model)
    {
        // do stuff
    }

我已尝试对JSON进行字符串化,在我的Angular帖子中将其作为data: member传递,并在ASP.NET MVC方法上使用[FromBody]但始终使用{{1} } properties为null。

无论如何我可以在不将每个属性发布到ASP.NET MVC控制器的情况下执行此操作吗?

编辑:为清晰起见,添加了MemberModel POCO

model.Address

1 个答案:

答案 0 :(得分:-1)

确保使用控制器方法上的[HttpPost]属性