2017-08-23 11 views
0

私は次のアヤックススクリプトは方法MVCコントローラにjsonArrayを送る

$(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); 
         } 
        }); 
       }); 
      }); 
このスクリプトは、パラメータとして一覧にコントローラに

[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スクリプトを実行すると、私はコントローラメソッドにリダイレクトされ、debbugerにはそのパラメータが表示されますIList参加者はnullですか?理由は何でしょうか?

+0

は、単純な '文字列[]'を使用して、あなたがかもしれない@Uphar –

+3

それをデバッグしようクレイジー。そこに 'var'を置くことはできません。 –

+0

string []同じ結果 –

答えて

3

あなたは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); 
    } 
}); 
関連する問題