获取空对象数组jquery post

本文关键字:jquery post 数组 对象 获取 | 更新日期: 2023-09-27 18:17:08

我是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。我哪里出错了?

获取空对象数组jquery post

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

查看答案

你的代码错了:

data: { AppSetting: $scope.items }

应该是

data: $scope.items

在你的saveClick函数中:$scope。项目现在是[]。它应该像你期望的那样有一些值。根据您的情况,它是来自客户端还是测试的默认值:

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