在我的“创建视图”中填充所有字段后,单击“提交”并在我的控制器的HttpPost Create方法中获取异常,该方法指出“字段'rework_code'没有默认值”。在控制器上方添加一个断点后,我可以看到传递给create方法的类对象是完全填充的(没有空字段)所以我很困惑为什么我得到这个异常。我正在与MySQL数据库连接,并且连接字符串似乎正在起作用,因为Index和Edit方法可以无缝地工作。
而且要清楚它是在db.SaveChanges()
上抛出异常控制器创建:
// POST: /DriveReworkCodes/Create
[HttpPost]
public ActionResult Create(Drive_Rework_Codes drive_rework_codes)
{
if (ModelState.IsValid)
{
db.Drive_Rework_Codes.Add(drive_rework_codes);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(drive_rework_codes);
}
类别:
namespace FactoryManager.Models
{
public class Drive_Rework_Codes
{
[Key]
[Display(Name = "Rework Code")]
public int Rework_Code { get; set; }
public int Action { get; set; }
public string Description { get; set; }
}
}
堆栈追踪:
at System.Data.Entity.Internal.InternalContext.SaveChanges()
at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
at System.Data.Entity.DbContext.SaveChanges()
at FactoryManager.Controllers.DriveReworkCodesController.Create(Drive_Rework_Codes drive_rework_codes) in c:\AAS_SDK\Projects\Training\WebApp1\FactoryManager\FactoryManager\Controllers\DriveReworkCodesController.cs:line 55
at lambda_method(Closure , ControllerBase , Object[] )
at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41()
at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49()