在执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪,以获取有关错误及其在代码中起源的更多信息。
public class HomeController : Controller
{
[HttpGet]
[ActionName("Registration")]
public ActionResult Registration_Get()
{
//Contry();
return View();
}
[HttpPost]
[ActionName("Registration")]
public ActionResult Registration_Post()
{
Registration register = new Registration();
TryUpdateModel(register);
if (ModelState.IsValid)
{
AddStudent(register);
return RedirectToAction("Registration");
}
return View(register);
}
public ActionResult AddStudent(Registration r)
{
OQContext db = new OQContext();
db.Registrations.Add(r);
db.SaveChanges();
return View();
}
}
我想将表单数据保存到数据库表,但是 db.Registrations.Add(r)上的 AddStudent 操作方法中发生了异常;
答案 0 :(得分:0)
将控制器更改为
[HttpGet]
[ActionName("Registration")]
public ActionResult Registration_Get()
{
//Contry();
return View();
}
[HttpPost]
[ActionName("Registration")]
public ActionResult Registration_Post(Registration r)
{
//This needs to come from the view
//Registration register = new Registration();
//TryUpdateModel(r);
if (ModelState.IsValid)
{
AddStudent(r);
return RedirectToAction("Registration");
}
return View(r);
}
您尚未从视图中传递模型
答案 1 :(得分:-2)
更改这样的控制器方法:
public ActionResult AddStudent()
{
return View();
}
[HttpPost]
public ActionResult AddStudent(Registration r)
{
try
{
OQContext db = new OQContext();
db.Registrations.Add(r);
db.SaveChanges();
}
catch(Exception ex)
{
}
return View();
}