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.

2

2 Answers 2

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)

Sign up to request clarification or add additional context in comments.

1 Comment

+1 for good link. The function to be executed is JSON.stringify(x).
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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.