0

I'm using Entity Framework and I want Admin to approve/reject a request in the Details method using two buttons (one for approve and another for reject). Approve is true and reject is false. After the click on one of the buttons I want it to change the field of RequestStatus from submitted to approve/reject but I'm getting an error in db.SaveChanges(). I've been searching that error but still I don't get it. Can someone explain what does this error means?

System.Data.Entity.Infrastructure.DbUpdateConcurrencyException: 'Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded.

Here is the Requests Model

public class Request
      {
            [Key]
            [Display(Name = "N. Requisição")]
            [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
            public int RequestID { get; set; }

            [Display(Name = "Utilizador")]
            public string User { get; set; }

            [Display(Name = "Data")]
            [DataType(DataType.Date)]
            [DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
            public System.DateTime RequestDate { get; set; }

            [Display(Name = "Comentários")]
            public string RequestComments { get; set; }

            [Display(Name = "Quantidade"), Required]
            public int ItemCount { get; set; }

            [Display(Name = "Material")]
            public string ItemDescription { get; set; }

            [Display(Name = "Estado")]
            public string RequestStatus { get; set; }



        }

Here is the Details Method

public ActionResult Details(int? id)
            {
                if (id == null)
                {
                    return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
                }
                Request request = db.Requests.Find(id);
                if (request == null)
                {
                    return HttpNotFound();
                }
                return View(request);
            }

            [HttpPost]
            [ValidateAntiForgeryToken]
            public ActionResult Details([Bind(Include = "RequestID,User,RequestDate,RequestComments,ItemCount,ItemDescription,RequestStatus")] Request request)
            {
                bool Status = true;

                if (Status)
                {
                    request.RequestStatus = "Aprovada";
                    db.Entry(request).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }

                return View(request);
            }


Here is the Details View


@model OnlineRequests.Models.Request

@{
/**/
ViewBag.Title = "Details";
}

<h2>Detalhes</h2>
@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

<div>
    <h4>Requisição</h4>
    <hr />
    <dl class="dl-horizontal">
        <dt>
            @Html.DisplayNameFor(model => model.User)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.User)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.RequestDate)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.RequestDate)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.RequestComments)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.RequestComments)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.ItemCount)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.ItemCount)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.ItemDescription)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.ItemDescription)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.RequestStatus)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.RequestStatus)
        </dd>

    </dl>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Aprovar" class="btn btn-success" />
        </div>
    </div>
</div>
}
<p>
    @Html.ActionLink("Editar", "Edit", new { id = Model.RequestID }) |
    @Html.ActionLink("Back to List", "Index")
</p>
bolkay
  • 1,881
  • 9
  • 20
Rosado
  • 1
  • 1
  • The error says you are trying to update and item which has been modified in between your select and edit request. These are the few cases like After select changing the entity and not passing the Primary key which will cause update as new entry but it will not allow. So check these facts. – Samaritan_Learner May 17 '19 at 11:23
  • Possible duplicate of [Store update, insert, or delete statement affected an unexpected number of rows (0) EntityFramework](https://stackoverflow.com/questions/20044828/store-update-insert-or-delete-statement-affected-an-unexpected-number-of-rows) – bolkay May 17 '19 at 11:26
  • If my guess is correct you are not passing the Primary key value – Samaritan_Learner May 17 '19 at 11:39
  • So I need to put an Html.HiddenFor(model => model.RequestID)? – Rosado May 17 '19 at 11:47

0 Answers0