繁体   English   中英

无法更新MVC 4中的模型

[英]Unable to update model in MVC 4

我正在尝试更新实体,但它给我一个错误,例如“存储更新,插入或删除语句影响了意外的行数(0)。实体加载后,实体可能已被修改或删除。刷新ObjectStateManager条目。 ” 我已经搜索了互联网,但是找不到任何合适的解决方案。 我正在分享我的代码:

动作方法:

[HttpPost, ValidateAntiForgeryToken]
        public async Task<ActionResult> EditCustomTicket([Bind(Include = @"TicketDetailId,GenericOrderId,PartId,Quantity,CustomerPrice,
                                                                             Status,RowVersion,PersonID,Notes,Manufacturer,DateCode,
                                                                             Package,CustomQuantity,BuyingPrice,CustomPrice,LTDays,
                                                                             Description")] TicketDetail orderdetail, int? id, string request)
        {
            TryUpdateModel(orderdetail);
            try
            {

                if (ModelState.IsValid)
                {
                    db.TicketDetails.Attach(orderdetail);
                    var manager = ((IObjectContextAdapter)db).ObjectContext.ObjectStateManager;

                    manager.ChangeObjectState(orderdetail, EntityState.Modified);

                    await db.SaveChangesAsync();
                    return RedirectToAction("Details", new { id = id });
                }

            }

            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message);
                TempData["Error"] = string.Format(ex.Message + "\n" + ex.Source);
                return RedirectToAction("Details", new { id = id });
            }
            return View(orderdetail);
        }

这是视图:

<div>
                @foreach (var item in Model)
                {
                    <form method="post" action="@Url.Action("EditCustomTicket")">

                        @Html.AntiForgeryToken()
                        @Html.ValidationSummary(true)

                        @Html.HiddenFor(model => item.TicketDetailId)
                        @Html.HiddenFor(model => item.GenericOrderId)
                        @Html.HiddenFor(model => item.Notes)
                        @Html.HiddenFor(model => item.Status)
                        @Html.HiddenFor(model => item.PersonID)
                        @Html.HiddenFor(model => item.RowVersion)
                        @Html.HiddenFor(model => item.Quantity)
                        @Html.HiddenFor(model => item.CustomerPrice)
                        @Html.HiddenFor(model => item.PartId)


                        <div class="form-group float">
                            @Html.LabelFor(model => item.Part.PartNumber, new { @class = "control-label col-md-2 lab" })
                            <div class="col-md-10">
                                @Html.DisplayFor(modelitem => item.Part.PartNumber)
                            </div>
                        </div>

                        <div class="form-group float">
                            @Html.LabelFor(model => item.Manufacturer, new { @class = "control-label col-md-2 lab" })
                            <div class="col-md-10">
                                @Html.EditorFor(model => item.Manufacturer, new { htmlattributes = new { @class = "textbox" } })
                                @Html.ValidationMessageFor(model => item.Manufacturer)
                            </div>
                        </div>
                        <div class="form-group float">
                            @Html.LabelFor(model => item.DateCode, new { @class = "control-label col-md-2 lab" })
                            <div class="col-md-10">
                                @Html.EditorFor(model => item.DateCode, new { htmlattributes = new { @class = "textbox" } })
                                @Html.ValidationMessageFor(model => item.DateCode)
                            </div>
                        </div>

                        <div class="form-group float">
                            @Html.LabelFor(model => item.Package, new { @class = "control-label col-md-2 lab" })
                            <div class="col-md-10">
                                @Html.EditorFor(model => item.Package, new { htmlattributes = new { @class = "textbox" } })
                                @Html.ValidationMessageFor(model => item.Package)
                            </div>
                        </div>
                        <div class="form-group float">
                            @Html.LabelFor(model => item.Quantity, new { @class = "control-label col-md-2 lab" })
                            <div class="col-md-10">
                                @Html.DisplayFor(modelitem => item.Quantity)
                            </div>
                        </div>


                        <div class="form-group float">
                            @Html.LabelFor(model => item.Package, new { @class = "control-label col-md-2 lab" })
                            <div class="col-md-10">
                                @Html.EditorFor(model => item.Package, new { htmlattributes = new { @class = "textbox" } })
                                @Html.ValidationMessageFor(model => item.Package)
                            </div>
                        </div>

                        <div class="form-group float">
                            @Html.LabelFor(model => item.BuyingPrice, new { @class = "control-label col-md-2 lab" })
                            <div class="col-md-10">
                                @Html.EditorFor(model => item.BuyingPrice, new { htmlattributes = new { @class = "textbox" } })
                                @Html.ValidationMessageFor(model => item.BuyingPrice)
                            </div>
                        </div>


                        <span id="bpusd"></span>

                        <div class="form-group float">
                            @Html.LabelFor(model => item.CustomPrice, new { @class = "control-label col-md-2 lab" })
                            <div class="col-md-10">
                                @Html.EditorFor(model => item.CustomPrice, new { htmlattributes = new { @class = "textbox" } })
                                @Html.ValidationMessageFor(model => item.CustomPrice)
                            </div>
                        </div>
                        <span id="amtusd"></span>
                        <div class="form-group float">
                            @Html.LabelFor(model => item.LTDays, new { @class = "control-label col-md-2 lab" })
                            <div class="col-md-10">
                                @Html.EditorFor(model => item.LTDays, new { htmlattributes = new { @class = "textbox" } })
                                @Html.ValidationMessageFor(model => item.LTDays)
                            </div>
                        </div>

                        <br />


                        <input class="btn green butt" value="Send Quote" type="submit" />
                        <input class="btn yellow butt" value="Send Performa INV." type="submit" />
                        <input class="btn blue butt" value="Send Invoice" type="submit" />



                        <hr />
                    </form>
                }
            </div>

这是局部视图。 请以此指导我。

当数据未更改或您不包括主键字段时,可能会发生这种情况。

您没有附加到现有实体。

您需要先指定要使用的实体,然后才能将票证详细信息附加到该实体,例如

var existingTicket = db.TicketDetails.Find(Id);

existingTicket.Attach(orderdetail);

尝试删除您的

EditCustomTicket

方法。 您可以尝试其他方法来处理批量分配问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM