Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Here is JSON Code to post that array to GetBulk Method:

$("#button").click(function () {
    var array = [
        {
            StudentRecordId: 1,
            Name: "Amit",
            Marks: 11,
            Grade: "A"
        },
        {
            StudentRecordId: 2,
            Name: "Abhishek",
            Marks: 12,
            Grade: "A"
        },
        {
            StudentRecordId: 3,
            Name: "Vipin",
            Marks: 13,
            Grade: "A"
        }
    ]

    $.ajax({
        type: "Post",
        url: "/Home/GetBulk",
        dataType: "json",
        traditional:true,
        data: JSON.stringify({ data: array }),
        traditional: true,//"feedGraphId=10696",
        success: function (result) {
            alert("j= " + result.studentRecord);                 
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert("error" + errorThrown);
        }
    });
});                

Here is method of my controller:

//to recieve array from json and post values to student record table
public JsonResult GetBulk(List<StudentRecord> models)
{
    StudentRecord studentRecords = new StudentRecord();

    foreach(var item in models)
    {
        studentRecords.Name = item.Name;
        studentRecords.Marks = item.Marks;
        studentRecords.Grade = item.Grade;
        db.StudentRecords.Add(studentRecords);
        db.SaveChanges();
    }

    var c = db.StudentRecords.ToList();
    return Json(models, JsonRequestBehavior.AllowGet);
}

Here is my Model:

public class StudentRecord
{
    public long StudentRecordId { get; set; }
    public string Name { get; set; }
    public int Marks { get; set; }
    public string Grade { get; set; }
}

So then how can I submit my values to a table using this json array?

share|improve this question

1 Answer 1

up vote 4 down vote accepted

You forgot to set the contentType of your AJAX request and also sent incorrect JSON payload (your server expects an array but you sent it as an object: {"data":[...]}):

$.ajax({
    type: 'POST',
    url: '/Home/GetBulk',
    contentType: 'application/json',
    data: JSON.stringify(array),
    success: function (result) {
        alert("j= " + result.studentRecord);
    },
    error: function (XMLHttpRequest, textStatus, errorThrown) {
        alert("error" + errorThrown);
    }
});

Other things you may notice in my code:

  • gotten rid of the dataType: 'json' -> it's unnecessary if the server sets the correct Content-Type response header (which it will if you return a Json result). In this case jQuery will use this response header to know how to process the response data
  • gotten rid of the traditional: true parameter -> it's useless when you are sending a JSON request which is what you are doing here
  • replaced JSON.stringify({ data: array }) with JSON.stringify(array) because with the first your data will be send like this: { "data": [...] } whereas your server expects an List<StudentRecord> so the payload should look like this: [...]
share|improve this answer
    
Shouldn't the data be JSON.stringify({ models: arrary }) so the model binder will be able to match the action method parameter with the value from the request? –  asymptoticFault Oct 3 '13 at 13:55
    
@Darin thanks a lot its work for me...can i have your mail so that i can ask my problem there –  Neeraj Mehta Oct 3 '13 at 17:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.