Note that there are some explanatory texts on larger screens.

plurals
  1. POUnable to update MVC3 Model - InvalidOperationException
    primarykey
    data
    text
    <p>I am fairly new to prgramming in general. I have a database with 3 tables in it. A table called <code>User</code> with a foreign Key <code>TypeID</code> null, a table called <code>Login</code> that has a foreign key <code>UserID</code> null and <code>TypeID</code> not null, and a table called <code>Types</code> with <code>TypeID</code> as the primary key. When I tried to update my entity Model, I receive an <code>Invalid InvalidOperationException</code>. </p> <pre><code>at System.Data.Objects.ObjectStateManager.ChangeObjectState(Object entity, EntityState entityState) at MBMVCApplication.Controllers.LoginController.Edit(Users user, Int32 TypeID) in C:\Development\MBMVCApplication\Controllers\LoginController.cs:line 130 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.ControllerActionInvoker.&lt;&gt;c__DisplayClass15.&lt;InvokeActionMethodWithFilters&gt;b__12() at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) </code></pre> <p><strong>Code</strong></p> <pre><code>[HttpPost] public ActionResult Edit(Login login, int? typeID) { if (ModelState.IsValid) { db.Logins.Attach(login); //TypeID display on a dropdown box //I also remove the two line below and still get the error login.User.TypeID = typeId.Value; db.ObjectStateManager.ChangeObjectState(login, EntityState.Modified); //The error occurs here or db.SaveChanges(); //Here return RedirectToAction("Index"); } ViewBag.UserID = new SelectList(db.Users, "UserID", "FirstName", login.UserID); ViewBag.TypeID= new SelectList(db.Types, "TypeID", "Description", login.TypeID); return View(login); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload