1

I have problem with savechanges. This code changes value after click, but it doesn't save this to database. Any ideas?

View:

@foreach (var item in Model)
{
  <center>
    <div class="acomp">
      <div class="comp">
        @Html.DisplayFor(modelItem => item.Content)
        @Ajax.ActionLink("ClickMe", "IncrementRating", new { compareId = item.ID }, new AjaxOptions
        {
          HttpMethod = "POST",
          UpdateTargetId = "Rating_" + @item.ID,
          InsertionMode = InsertionMode.Replace
        })
        <div id="Rating_@item.ID">@Html.DisplayFor(modelItem => item.Rating</div>
      </div>
    </div>
  </center>
}

Controller:

[HttpPost]
public PartialViewResult IncrementRating(int compareId)
{
    var comparison = db.Comparisons.Find(compareId);
    comparison.Rating += 1;
    db.Entry(comparison).State = EntityState.Modified;
    db.Comparisons.Attach(comparison);
    db.SaveChanges();
    return PartialView("_Voting", comparison);
}

Partial view: _Voting

@model Compare.Models.Comparison
@Html.DisplayFor(model=>Model.Rating)
  • Have you tried to remove the line with state.modified and instead of attach to use add? – adricadar Apr 05 '15 at 10:30
  • Yes, but it still doesn't work. At the line with "saveChanges()" is an error "An exception of type 'System.Data.Entity.Validation.DbEntityValidationException' occurred in EntityFramework.dll but was not handled in user code " and detail: "EntityValidationError: Count=1" – Paweł Jurczyński Apr 05 '15 at 10:35

1 Answers1

0

Perhaps the validation error occurs when attaching the object as you do. Does the same error ocur when you do it somehow like this?

[HttpPost]
public PartialViewResult IncrementRating(int compareId)
{
    var comparison = db.Comparisons.SingleOrDefault(o => o.id == comapreId);

    comparison.Rating += 1;
    //Other edit sutff......
    //db.Comparisons.Attach(comparison);

    db.SaveChanges();

    return PartialView("_Voting", comparison);
}

I know, this should have been a comment rather than answer but it came out as a comment not good for reading.

gardarvalur
  • 1,565
  • 9
  • 39
  • 65
  • It still doesn't work. First, comparison is an object, not a collection, so I have to write for example db.Comparisons.SingleOrDefault(o => o.id == comapreId).FirstOrDefault(), but its the same as db.Comparisons.Find(compareId); Code, what i wrote at first works, with no errors but it doesn't save data to database. Maybe it something wrong with ajax, i have no idea.. – Paweł Jurczyński Apr 05 '15 at 12:03
  • Hmmmm... nothing should be wrong with your ajax call if the compareId is submitted to the server. Does the id submit correctly to the server? Another question, have you tried updating other values of the comparison object aswell, could it possibly be that the context isn't the same as the attached one? – gardarvalur Apr 05 '15 at 16:44
  • At least from the error you displayed above it seems as the context is complaining about database validation from the entity. How about testing the savechanges after you have hardcoded changes to each property of the comparison entity. – gardarvalur Apr 05 '15 at 16:59
  • It doesn't update other data too. Method is passing a good object with good values. But it still doesn't save.. – Paweł Jurczyński Apr 05 '15 at 18:24
  • Maybe you could try to create a partial class for the entity and override the SaveChanges to see what the error really means as suggested in this answer: http://stackoverflow.com/questions/15820505/dbentityvalidationexception-how-can-i-easily-tell-what-caused-the-error – gardarvalur Apr 05 '15 at 23:20