【问题标题】:JSON to object does not working on ajax callJSON to object 不适用于 ajax 调用
【发布时间】:2018-10-21 19:57:00
【问题描述】:

只是一个简单的问题。我不知道这段代码有什么问题我无法将 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 调用时,我总是在我的属性上得到空值,并且数据没有正确传输。

【问题讨论】:

  • 你遇到了什么错误?
  • 本身不是错误,但我没有从 DownloadReportViewModel 获得值。
  • 我更新了我的帖子。

标签: c# json ajax


【解决方案1】:

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

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

【讨论】:

  • 尝试从您的代码中删除 json.stringfy 并按原样发送对象
猜你喜欢
  • 2015-01-16
  • 2018-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-04
  • 1970-01-01
  • 2021-08-13
相关资源
最近更新 更多