错误存储,更新,插入或删除语句影响了意外的行数(0)

时间:2019-05-17 11:06:36

标签: c# asp.net-mvc

我正在使用Entity Framework,并且我希望Admin使用两个按钮(一个用于批准,另一个用于拒绝)批准/拒绝Details方法中的请求。批准为真,拒绝为假。单击其中一个按钮后,我希望它将RequestStatus的字段从“提交”更改为“批准/拒绝”,但是我在db.SaveChanges()中遇到错误。我一直在搜索该错误,但仍然不明白。有人可以解释这个错误是什么意思吗?

  

System.Data.Entity.Infrastructure.DbUpdateConcurrencyException:'存储   更新,插入或删除语句影响了意外数量的   行(0)。自实体以来,实体可能已被修改或删除   已加载。

这是请求模型

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; }



        }

这是“详细信息”方法

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>

0 个答案:

没有答案