我试图获取表单集合值并插入数据库。但我面临的典型问题是第一行只插入数据库。我的代码如下:
[HttpPost]
public ActionResult Create(FormCollection Students)
{
if (ModelState.IsValid)
{
string[] names = Students["name"].Split(char.Parse(","));
string[] dnos = Students["dno"].Split(char.Parse(","));
string[] adds = Students["address"].Split(char.Parse(","));
student stds = new student();
for (var i = 0; i < names.Length; i++)
{
stds.name = names[i];
stds.dno = dnos[i];
stds.address = adds[i];
stds.active = true;
db.students.AddObject(stds);
}
db.SaveChanges();
return RedirectToAction("Index");
}
return View(Students);
}
请帮助解决这个问题。