JSON不能在ajax调用上起作用

时间:2018-10-21 19:57:01

标签: c# json ajax

一个简单的问题。我不知道这段代码有什么问题,我无法将json文件作为对象传递给控制器​​。

这是Javascript的样子。

var reportparams = {
                Client_Name_Short: $("#Client_Name_Short").val(),
                CountryNo: $("#CountryNo").val(),
                ProfileUID: $("#ProfileUID").val(),
                HidEmployerList: $("#HidEmployerList").val(),
                MarketPositions: $("#MarketPositions").val(),
                CurrencyID: $('#CurrencyID').val(),
                Language: $('#Language').val(),
                JobMatches: '0',
                ReportType: 'Country_or_Ngolp',
                ShowIncumbents: $("#ShowIncumbents").is(":checked"),
                ExcludeSelectedEmployer: $("#ExcludeSelectedEmployer").is(":checked")
            };
            var reportparamsJSON = JSON.stringify(reportparams);
            $.ajax({
                url: 'LoadPortfolio',
                type: 'POST',
                dataType: 'json',
                data: reportparamsJSON,
                contentType: 'application/json; charset=utf-8',
                success: function (data) {
                    alert(data);
                }
            });

这是控制器的样子。

[HttpPost]
[ActionName("LoadPortfolio")]
public async Task<IActionResult> LoadPortfolioCall(DownloadReportViewModel model){...}

这是模型的外观。

[Display(Name ="Select Client Name: ")]
        public string Client_Name_Short { get; set; }
        [Display(Name = "Select Country: ")]
        public int CountryNo { get; set; }
        [Display(Name = "Select Profile: ")]
        public string ProfileUID { get; set; }
        [Display(Name = "Select Employer: ")]
        public string HidEmployerList { get; set; }
        [Display(Name = "Select Market Positions: ")]
        public string MarketPositions { get; set; }
        [Display(Name = "Select Currency: ")]
        public string CurrencyID { get; set; }
        [Display(Name = "Language: ")]
        public string Language { get; set; }
        public string JobMatches { get; set; }
        public string ReportType { get; set; }
        [Display(Name = "Show Incumbents: ")]
        public bool ShowIncumbents { get; set; }
        [Display(Name = "Exclude Selected Employer: ")]
        public bool ExcludeSelectedEmployer { get; set; }

        #region View
        public List<SelectListItem> ClientNameSelection { get; set; }
        public List<SelectListItem> CountrySelection { get; set; }
        public List<SelectListItem> ProfileSelection { get; set; }
        #endregion

        public DownloadReportViewModel()
        {
            this.ClientNameSelection = new List<SelectListItem>();
            this.CountrySelection = new List<SelectListItem>();
            this.ProfileSelection = new List<SelectListItem>();
        }

每当我尝试通过ajax调用时,我总是在属性中获得空值,并且数据无法正确传输。

1 个答案:

答案 0 :(得分:0)

尝试将ajax调用中的数据设置为对象 这样

$.ajax({
                url: 'LoadPortfolio',
                type: 'POST',
                dataType: 'json',
                data: {model:reportparamsJSON},
                contentType: 'application/json; charset=utf-8',
                success: function (data) {
                    alert(data);
                }
            });