【问题标题】:Send jsonArray to method MVC controller将 jsonArray 发送到方法 MVC 控制器
【发布时间】:2017-08-23 08:48:54
【问题描述】:

我有以下 ajax 脚本

$(function () {
                $("#btnConfirmParticipants").click(function () {
                    var jsonArray = [];
                    $(".form-horizontal").each(function () {
                        jsonArray.push({ Name: ($(this).find("#Name").val()), Surname: ($(this).find("#Surname").val()), BirthDate: ($(this).find("#BirthDate").val()) });
                    });
                    $.ajax({
                        type: "POST",
                        url: "/ClientReservations/AddParticipants",
                        data: JSON.stringify(jsonArray),
                        dataType: 'application/json; charset=utf-8',
                        success: function (response) {

                        },
                        failure: function (response) {
                            alert(response.responseText);
                        },
                        error: function (response) {
                            alert(response.responseText);
                        }
                    });
                });
            });

该脚本负责在控制器中执行以List为参数的方法

[HttpPost]
        [Authorize(Roles ="Klient")]
        public ActionResult AddParticipants(IList<Participant> participants)
        {
            return View();
        }

模型参与者看起来像这样

public class Participant
    {

        public Participant()
        {
            this.Reservation_House_Participant = new HashSet<Reservation_House_Participant>();
        }

        [Display(Name ="Id:")]
        [Required]
        public int Id { get; set; }

        [Display(Name ="Imię:")]
        [Required]
        [MinLength(3),MaxLength(15)]
        public string Name { get; set; }

        [Display(Name ="Nazwisko:")]
        [Required]
        [MinLength(3),MaxLength(15)]
        public string Surname  { get; set; }

        [Display(Name ="Data urodzenia:")]
        [Required]
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString ="{0:dd-MM-yyyy}",ApplyFormatInEditMode =true)]
        public DateTime BirthDate { get; set; }
}

当我单击按钮执行 ajax 脚本时,它会将我重定向到控制器方法,并且在调试器中我看到参数 IList 参与者 为空?可能是什么原因?

【问题讨论】:

  • 尝试使用简单的string[],然后调试
  • @Uphar 你可能疯了。你不能把var放在那里。
  • string[] 结果一样
  • 也许我应该使用 nuGet 导入一些 json 库?
  • @Contador6 等一下,我正在调试它

标签: javascript asp.net-mvc asp.net-mvc-4 asp.net-ajax


【解决方案1】:

你的jQuery有错误,你需要设置contentType,而不是dataType属性:

$.ajax({
    type: "POST",
    url: "/ClientReservations/AddParticipants",
    data: JSON.stringify(jsonArray),
    contentType: 'application/json; charset=utf-8', //<-- this line
    success: function (response) {

    },
    failure: function (response) {
        alert(response.responseText);
    },
    error: function (response) {
        alert(response.responseText);
    }
});

【讨论】:

    猜你喜欢
    • 2019-05-24
    • 2012-09-09
    • 1970-01-01
    • 1970-01-01
    • 2019-05-31
    • 2012-01-29
    • 2020-12-30
    • 2016-09-07
    • 2018-06-01
    相关资源
    最近更新 更多