Note that there are some explanatory texts on larger screens.

plurals
  1. POLINQ has trouble updating entities from model binding
    text
    copied!<p>I have an edit form with ExtJs but ExtJs is not relevant for this problem. On submit, I bind the form to a model.</p> <pre><code>public ActionResult EditUserHour([Bind(Include = "Id, Duration, HourCategoryId, Explanation, InvoiceTypeId, StartDate, StartTime, TicketId")] UserHour userHour) { if (ModelState.IsValid) { try { _service.Update(userHour); } catch (RuleException ex) { ex.CopyToModelState(ModelState, string.Empty); } } if (ModelState.IsValid) return Json(new { success = true, redirect = Url.Action(ListAction) }); else return Json(new { success = false, errors = ModelState.ToDictionary() }); } </code></pre> <p>Look at the _service.Update(userHour); line. Between the controller and the repository is a service layer so I don't call the repository directly. I consider this a better design than mixing data access, validation and business logic in a repository.</p> <p>Currently, the - not working - method in _service is:</p> <pre><code>public void Update(UserHour userHour) { CRMDataContext c = new CRMDataContext(); c.Refresh(RefreshMode.KeepCurrentValues, userHour); c.SubmitChanges(); } </code></pre> <p>I have tried everything from all c.Attach(...) calls possible and refresh calls, but I get various exceptions like attaching an object which has a key which already exists. </p> <p>One of the possible solutions I have encountered is retrieving the <em>original</em> entity from datacontext and simply set all the properties, but that's far from a neat solution. A second option is to use FormCollection to map to the <em>original</em> entity, but I avoid using FormCollection and prefer model binding due to security reasons. Besides, I cannot imagine model binding is incompatible with update. </p> <p>Is there a possibility of creating a userHour in the controller with model binding, giving it the identity of the userHour it is actually updating and store it in the database? It has bugged me all day long by now.</p> <p>Any help is greatly appreciated!</p>
 

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