1
這裏是JSON代碼張貼該數組GETBULK方法:如何提交JSON陣列到數據庫表在ASP.Net MVC4
$("#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數組?
應該不是數據是'JSON.stringify({機型:arrary} )'所以模型聯編程序將能夠匹配action方法參數和來自reque的值ST? – asymptoticFault
@Darin非常感謝我的工作......我可以收到你的郵件,以便我可以在那裏問我的問題 –