这是将该数组发布到 GetBulk 方法的 JSON 代码:
$("#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);
}
});
});
这是我的控制器的方法:
//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);
}
这是我的模型:
public class StudentRecord
{
public long StudentRecordId { get; set; }
public string Name { get; set; }
public int Marks { get; set; }
public string Grade { get; set; }
}
那么如何使用这个 json 数组将我的值提交到表中呢?