简体   繁体   English

当前Web请求的执行期间发生未处理的异常。 型号兼容性无法检查

[英]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.

相关问题 执行当前 Web 请求期间发生未处理的异常。 ASP.NET - An unhandled exception occurred during the execution of the current web request. ASP.NET 如何修复“在执行当前 Web 请求期间发生未处理的异常。” - How to fix "An unhandled exception occurred during the execution of the current web request. " 在执行当前Web请求期间生成了未处理的异常。[HttpAntiForgeryException] - An unhandled exception was generated during the execution of the current web request.[HttpAntiForgeryException] 当前Web请求“Ненайденуказанныймодуль”的执行期间发生未处理的异常 - An unhandled exception occurred during the execution of the current web request, “Не найден указанный модуль” 当前Web请求执行期间发生未处理的异常 - An unhandled exception occurred during the execution of the current web request 获取“在执行当前Web请求期间生成了未处理的异常。” 我的MVC UserManagementController中的错误 - Getting 'An unhandled exception was generated during the execution of the current web request.' Error in my MVC UserManagementController Sitecore MVC-在执行当前Web请求期间生成了未处理的异常 - Sitecore MVC - An unhandled exception was generated during the execution of the current web request 在执行当前Web请求asp net的过程中生成了未处理的异常 - an unhandled exception was generated during the execution of the current web request asp net 当前Web请求执行期间生成了未处理的异常 - An unhandled exception was generated during the execution of the current web request 执行过程中发生未处理的异常 - An unhandled exception occurred during the execution
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM