[英]An unhandled exception occurred during the execution of the current web request. Model compatibility cannot be checked
An unhandled exception occurred during the execution of the current web request.
当前Web请求的执行期间发生未处理的异常。 Please review the stack trace for more information about the error and where it originated in the code.
请查看堆栈跟踪,以获取有关错误及其在代码中起源的更多信息。
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();
}
}
I want to save form data to database table but an exception occure in AddStudent action method on db.Registrations.Add(r); 我想表单数据保存到数据库表中,但传递addStudent操作方法异常occure上db.Registrations.Add(R);
Change your controller to 将您的控制器更改为
[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);
}
You are not yet passing the model from the view 您尚未从视图传递模型
Change controller methods like this : 更改控制器方法如下:
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.