4

javascript\jQuery:

 var items = new Array();

 var obj { Begin: "444", End: "end" };

 items.push(obj);
 items.push(obj);

  var request = {
             DateStart: $("#DateStart").val(),
             mass: items
         };


 $.post("/Home/Index", request, null,
 "json");

C# Mvc Index Controller

 public class MyClass
     {
        public string Begin;
        public string End;
     }

     [AcceptVerbs(HttpVerbs.Post)]        
     public ActionResult Index(            
         string DateStart,            
         MyClass []mass)
     {
         System.Diagnostics.Debug.WriteLine(mass[0].Begin);
     }

how to execute this code? thanks.

DaveRandom
  • 87,921
  • 11
  • 154
  • 174
mola10
  • 976
  • 2
  • 18
  • 31
  • possible duplicate of [jQuery Ajax POSTing array to ASP.NET MVC Controller](http://stackoverflow.com/questions/4402036/jquery-ajax-posting-array-to-asp-net-mvc-controller) – Meryovi Jul 18 '13 at 14:47
  • Here is the right http://theycallmemrjames.blogspot.com/2010/05/aspnet-mvc-and-jquery-part-4-advanced.html – mola10 Nov 27 '10 at 12:49

2 Answers2

1

U can't pass mass: items and expect it to be serialized as a JSON array automatically, you will need to either iterate and construct the JSON (bad plan) or use a JSON library(good plan)

Paul Creasey
  • 28,321
  • 10
  • 54
  • 90
0

Try write code as below:

var option = {
  url: '/Home/Index',
  type: 'POST',
  data: JSON.stringify(request),
  dataType: 'html',
  contentType: 'application/json',
  success: function(result) {
    alert(result);
  }
};
$.ajax(option);
Mickael Lherminez
  • 679
  • 1
  • 10
  • 29
roast_soul
  • 3,554
  • 7
  • 36
  • 73