该物业' Id'是对象的关键信息的一部分,不能在ASP.Net中使用EF进行修改

时间:2017-03-16 06:57:44

标签: javascript c# asp.net asp.net-mvc entity-framework

当我在ASP.NET中使用EF调用update时会抛出此错误:

  

System.InvalidOperationException:属性' Id'是对象关键信息的一部分,不能修改。

我很感激能为此提供一些帮助。

控制器代码: SaveUpdate方法代码:



        [HttpPost]
        public JsonResult CashVoucherSaveData(CashVoucherFormViewModel cashVoucherFormViewModel)
        {
            try
            {
                int? id = null;
                Voucher voucher;
                VoucherDetail voucherDetail;
                if (cashVoucherFormViewModel.Id == null)
                {
                    voucher = new Voucher();
                    voucherDetail = new VoucherDetail();
                }
                else
                {
                    id = (int)cashVoucherFormViewModel.Id;
                    voucher = _voucherService.GetVoucherById((int)id);
                    voucherDetail = _voucherDetailService.GetVoucherDetailById((int)id);
                }
                // voucherFormViewModel.Debit = 33;
                voucher = (Voucher)Mapper.Map(cashVoucherFormViewModel, voucher, typeof(CashVoucherFormViewModel), typeof(Voucher));
                voucherDetail = (VoucherDetail)Mapper.Map(cashVoucherFormViewModel, voucherDetail, typeof(CashVoucherFormViewModel), typeof(VoucherDetail));
                voucher.FinancialYearId = 2;
                voucher.CompanyId = 2;
                voucher.BusinessUnitId = 6;
                voucher.Prefix = "3";
                //voucherDetail.Serial = 3;
                // voucher.Code = "23";    // voucher #
                VoucherValidator voucherValidator = new VoucherValidator();
                VoucherDetailValidator voucherDetailValidator = new VoucherDetailValidator();
                ValidationResult validationResult = voucherValidator.Validate(voucher);
                ValidationResult validationResultVoucheDetail = voucherDetailValidator.Validate(voucherDetail);

                if (validationResult.IsValid && validationResultVoucheDetail.IsValid)
                {
                    if (id == null)
                    {
                        _voucherService.CreateVoucher(voucher);
                        _voucherDetailService.CreateVoucherDetail(voucherDetail);
                    }
                    else
                    {
                        _voucherService.Update(voucher);
                        _voucherDetailService.Update(voucherDetail);
                    }

                    _voucherService.SaveVoucher();
                    _voucherDetailService.SaveVoucherDetail();
                    return new JsonSuccessResult();
                }
                else
                {
                    Response.StatusCode = (int)ResponseCode.UnprocessableEntity;
                    return new JsonErrorResult(validationResult);
                }
            }
            catch (Exception ex)
            {
                Response.StatusCode = (int)ResponseCode.UnprocessableEntity;
                return new JsonErrorResult(ex.ToString());
            }
        }




查看代码:



@model RPS.WebApp.Areas.Accounting.ViewModels.CashVoucherFormViewModel
<div id="modal_backdrop" class="modal fade" data-backdrop="false">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
            </div>
            <div class="modal-body">
                <div class="text-center">
                    <h5 class="content-group-lg">Cash Payment Voucher</h5>
                </div>
            </div>

            @using (Html.BeginForm("CashVoucherSaveData", "Vouchers", FormMethod.Post, new { @class = "stepy-basic", @id = "frmVoucher", @role = "form" }))
            {
                @Html.AntiForgeryToken()
                @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                if (ViewBag.FormMode == FormMode.Edit)
                {
                    @Html.HiddenFor(model => model.Id)
                    @Html.HiddenFor(model => model.VoucherDetailId)
                }
                <fieldset title="1">
                    <legend class="text-semibold">Basic Info</legend>
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>Voucher Date </label>
                                @Html.EditorFor(model => model.VoucherDate, new { htmlAttributes = new { @class = "form-control daterange-single", @type = "text", @id = "pick_date", @placeholder = "Pick a Date" } })
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>Voucher #</label>
                                @Html.EditorFor(model => model.Code, new { htmlAttributes = new { @class = "form-control daterange-single", @type = "text", @readonly = "true", @id = "voucherCode" } })
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <!--Start Debit account Field-->
                        <div class="col-md-12 " id="DebitAcc">
                            <div class="form-group">
                                <label>Debit Account</label>
                                @Html.DropDownListFor(model => model.AccountId, new SelectList(Model.Accounts, "Id", "Name"), "-- Select Debit Account --", new { @class = "select required select2insidemodal", @id = "DACC" })
                            </div>
                        </div>
                        <!--End Debit account Field-->
                    </div>
                </fieldset>
                <fieldset title="2">
                    <legend class="text-semibold">Fill Voucher</legend>
                    <fieldset>
                        <div>
                            <div class="col-lg-1 col-lg-offset-9" style="padding-bottom:5px;padding-left:27px;">
                                <button type="button" class="btn btn-info" id="addMore">
                                    <span class="glyphicon glyphicon-plus"></span> Add More
                                </button>
                            </div>
                        </div>
                        @Html.EditorFor(model => model.Serial, new { htmlAttributes = new { @id = "serialCount" } })
                        <div class="row">
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label>Total Debit Amount</label>
                                    @Html.EditorFor(model => model.Debit, new { htmlAttributes = new { @class = "form-control daterange-single", @type = "text", @id = "debit", @readonly = true } })
                                </div>
                            </div>
                        </div>
                        <div id="dynamicBlock">
                        </div>
                            <hr />
                            <div class="row">
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label>Credit Account</label>
                                        @Html.DropDownListFor(model => model.AccountId, new SelectList(Model.Accounts, "Id", "Name"), "-- select account --", new { @class = "select required select2insidemodal", @id = "creditAccount" })
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label>Amount</label>
                                        @Html.EditorFor(model => model.Credit, new { htmlAttributes = new { @class = "form-control", @type = "text", @id = "credit" } })
                                    </div>
                                </div>
                            </div>
                            <div  id ="HiddenTemplate" class="dN">
                                
                                <div id="ROW-0" class="row">
                                    <hr />
                                    <div class="col-md-4">
                                        <div class="form-group">
                                            <label>Credit Account</label>
                                            @Html.DropDownListFor(model => model.AccountId, new SelectList(Model.Accounts, "Id", "Name"), "-- select account --", new { @class = "select required select2insidemodal", @id = "creditAccount" })
                                        </div>
                                    </div>
                                    <div class="col-md-4">
                                        <div class="form-group">
                                            <label>Amount</label>
                                            @Html.EditorFor(model => model.Credit, new { htmlAttributes = new { @class = "form-control", @type = "text", @id = "credit" } })
                                        </div>
                                    </div>
                                    <div class="col-md-4">
                                        <div class="form-group">
                                            <button type="button" id="btnRemove-0" class="btn bg-teal-400 stepy-finish">Remove <i class="icon-check position-right"></i></button>
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <div class="row">
                                <div class="col-md-12">
                                    <div class="form-group">
                                        <label>Narration</label>
                                        @Html.EditorFor(model => model.narration, new { htmlAttributes = new { @class = "form-control", @type = "text" } })
                                    </div>
                                </div>
                            </div>
                            <span> Created by:</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span>Created Date:</span>
</fieldset>
                </fieldset>
                <button type="button" id="btnSave" class="btn bg-teal-400 stepy-finish">Save <i class="icon-check position-right"></i></button>
            }
        </div>
    </div>
</div>
&#13;
&#13;
&#13;

Servicerepository适用于其他来电。

请帮我解决一下发生的事情。谢谢

0 个答案:

没有答案