首页 > 解决方案 > 将数据从 Angular 传递到 MVC 控制器反序列化一个空的 Viewmodel

问题描述

我正在对 Angular 项目进行一些维护,当将对象从控制器传递到服务时,我遇到了一个奇怪的行为......为简单起见,我将读取负载减少到只是一个字段

这是 Angular 控制器

  $scope.createNorm = function () {

    var norma = {};
    norma.IdType = -1;

    createDialog({
        title: "Creazione di una nuova norma",
        template: NormeService.dialogTemplate,
        okLabel: "Salva",
        controller: ["$scope", "NormeService", function ($scope, NormeService) {

            $scope.aziende = [];
            $scope.tipoNorme = [];

            var init = function () {

                NormeService.getCompanyList().then(function (response) {
                    var arr = response.data.Items || [];
                    var aziende = [];
                    for (var i = 0; i < arr.length; i++) {
                        aziende.push({
                            Text: arr[i].Value,
                            Value: arr[i].Id
                        });
                    }
                    $scope.aziende = aziende;
                });

                NormeService.getNormTypesList().then(function (response) {

                    var arr = response.data || [];
                    var tipoNorme = [];
                    for (var i = 0; i < arr.length; i++) {
                        tipoNorme.push({
                            Text: arr[i].NAME,
                            Value: arr[i].ID
                        });
                    }

                    console.log(tipoNorme);
                    $scope.tipoNorme = tipoNorme;
                });

            };
            init();

            $scope.norma = angular.copy(norma);
            $scope.value = $scope.norma;
        }]
    }).then(function (result) {
        $scope.save(result);
    }, function () { });
};

角服务

 save: function (data) {

            $http.post(baseUrl + "/save-norm2", { Norm:data});
        },

服务数据模板

  dialogTemplate: [
            '<advancedselect options="tipoNorme" label="Tipo norma" ng-model="norma.IdType" type="text" ng-required="false" form-model="frm" />', /*ng - required="true"*/

MVC 视图模型

public class TempViewModel
{
 public int IdType { get; set; }
}

和 MVC 控制器

   [System.Web.Http.HttpPost]
    [System.Web.Http.Route("save-norm2")]
    public IHttpActionResult SaveNorm(TempViewModel Norm)
    {
        return Ok(true);
    }

正如您从屏幕截图中看到的那样,我已经在浏览器部分填充了更正的值。

在此处输入图像描述

当它到达 MVC 部分的控制器时,它得到 0 作为 IdType

我究竟做错了什么?

标签: c#angularjsasp.net-mvc

解决方案


推荐阅读