获取Null对象数组jquery post

时间:2016-11-10 03:18:19

标签: javascript c# jquery angularjs

我是Angular.js框架的新手。我正在获取数据,我正在使用角度范围进一步分配给数组。

Modal.CS:

 public class AppSetting1
{
    public int lng_AppSettings { get; set; }
    public string str_SettingName { get; set; }
    public string str_SettingValue { get; set; }
    public string str_Type { get; set; }

    public AppSetting1(int _lng_AppSettings, string _str_SettingName, string _str_SettingValue, string _str_Type)
    {
        lng_AppSettings = _lng_AppSettings;
        str_SettingName = _str_SettingName;
        str_SettingValue = _str_SettingValue;
        str_Type = _str_Type;
    }
}

 internal string GetAppSettings()
        {
            try
            {

                    List<AppSetting1> objAppsettings = new List<AppSetting1>();
                    objAppsettings.Add(new AppSetting1(1,"Name1","Value1","Type"));
                    objAppsettings.Add(new AppSetting1(2, "Name2", "Value2", "Type2"));
                    return JsonConvert.SerializeObject(objAppsettings, Formatting.Indented, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

Controller.CS:

   [AuthCheckService, SessionCheckService]
    [HandleModelStateException]
    public string GetAppSettings()
    {
        try
        {
           ManageAppSettings accExec = new ManageAppSettings();
           return accExec.GetAppSettings();
        }
        catch (Exception ex)
        {
            throw new ModelStateException(ex.Message, ex.InnerException);
        }
    }

    [HttpPost]
    public JsonResult SaveSettings(List<AppSetting1> AppSetting)
    {
        try
        {
            ManageAppSettings accExec = new ManageAppSettings();
            return Json(AppSetting, JsonRequestBehavior.AllowGet);
        }
        catch (Exception ex)
        {
            throw new ModelStateException(ex.Message, ex.InnerException);
        }
    }

Angular.js:

(function () {
var app = angular.module('myAppSeetings', []);
app.controller('AppSettingsController', function ($scope) {

    $scope.items = [];
    $scope.SaveSettings = function () {
        if (validate()) {
            var token = $('[name=__RequestVerificationToken]').val();
            var test = $scope.items;
            $.ajax({
                beforesend: showProgress(),
                type: 'POST',
                headers: { "__RequestVerificationToken": token },
                url: getAppPath() + 'AppSettings/SaveSettings',
                dataType: 'json',
                data: { AppSetting: $scope.items },
                success: function (result) {
                    if (result != "") {
                        //ShowSaveMessage(result);
                        //fetchData();
                        //$('#EditPopUp').css('display', 'none');
                        //$('#exposeMaskManageUser').css('display', 'none');
                        //clearForm();
                    }
                    else
                        ShowErrorPopup('An error has occurred. Please contact System Administrator.');
                },
                complete: hideProgress,
                error: function (ex) {
                    ShowErrorPopup('An error has occurred. Please contact System Administrator.');
                }
            });
        }
        else {
            ShowWarningMessage('Required fields must be completed prior to completing the work');
        }
    };

    function fetchData() {

        var token = $('[name=__RequestVerificationToken]').val();
        $.ajax({
            beforesend: showProgress(),
            type: 'GET',
            headers: { "__RequestVerificationToken": token },
            url: getAppPath() + 'AppSettings/GetAppSettings',
            dataType: 'json',
            success: function (data) {
                // console.log(data);
                $scope.items = data;
                $scope.$apply();
                console.log($scope.items);
            },
            complete: hideProgress,
            error: function (ex) {
                ShowErrorPopup('An error has occurred. Please contact System Administrator.');
            }
        });
    };

    function validate() {
        var val = true;
        if ($("input").val().trim() == "") {
            val = false;
        }
        return val;
    }
    fetchData();
});

})();

问题:

在保存时单击我在服务器端获取null。我在哪里错了?

2 个答案:

答案 0 :(得分:1)

尝试添加 contentType: 'application/json; charset=utf-8',

See this answer

答案 1 :(得分:0)

你的代码错了:

data: { AppSetting: $scope.items }

应该是

data: $scope.items

在你的saveClick函数中:$ scope.items现在是[]。它应该有你想象的一些值。根据您的情况,它来自客户端或测试的默认值:

$scope.items = [{lng_AppSettings: 1, str_SettingName : 'Name 1'},
                {lng_AppSettings: 2, str_SettingName : 'Name 2'}];