Dex mateo
Dex mateo

Reputation: 23

MVC 5 inline edit Store update, insert, or delete statement affected an unexpected number of rows

I've been doing an inline editing in my mvc application but whenever I tried to update some database entries I got this error(mentioned in title). here's my javascript code in my index view:

function SaveChanges()
    { 
        var recs = [];
        var id = $('#lblID').text();
        var type = $('#lblType').text();
        var year = $('#txtYear').val();
        var bal = $('#txtBal').val();
        var rate = $('#txtRate').val();
        var bank = $('#drpBanks').val();
        var month = $('#1').text();
        var total = $('#txtTotal').text();
        var bitEdit = "1";

        var recs = {
            ID: id,
            nTypeId: type,
            nBudgetYear: year,
            nBegBalance: bal,
            nRate: rate,
            sBankName: bank,
            nPerMonth: month,
            nTotal: total,
            bEdit: bitEdit
        }

        $.ajax({
            url: "@Url.Action("Edit")",
            type: "POST", 
            data: JSON.stringify(recs),
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: function (response) {
               // alert(response.responseText);
                alert("Pasok sa success!");
            },
            error: function (response) {
                alert(response.responseText);
               // alert("Pasok sa error!");
            }
        });

    }

and this is my edit in my controller

       [HttpPost]
       public ActionResult Edit([Bind(Include = "sBankName, nType, nRate, nBegBal, nMonth, nTotal, nYear, dDateEntry, bEdited")]InputInterestIncome interest)
    {
        try
        {
            if (ModelState.IsValid)
            {
                db.Entry(interest).State = EntityState.Modified;
                db.SaveChanges();
                TempData["Msg"] = "Data has been updated successfully";
                return RedirectToAction("Index");
            }

            return View(interest);
        }

        catch (System.Data.Entity.Validation.DbEntityValidationException ex)
        {
            Exception raise = ex;
            foreach (var validationErrors in ex.EntityValidationErrors)
            {
                foreach (var validationError in validationErrors.ValidationErrors)
                {
                    string message = string.Format("{0}:{1}",
                        validationErrors.Entry.Entity.ToString(),
                        validationError.ErrorMessage);
                    // raise a new exception nesting  
                    raise = new InvalidOperationException(message, raise);
                }
            }
            throw raise;
        }

    }

What am I doing wrong here?

Upvotes: 1

Views: 680

Answers (1)

Dex mateo
Dex mateo

Reputation: 23

well for me this link works.. hope it'll help you too.

http://www.c-sharpcorner.com/Blogs/14611/insert-delete-update-read-crud-operation-in-mvc-4-0-using.aspx

think i got this error cos the ID isn't passed from the my Javascript to my Controller. Problem solved!

Upvotes: 1

Related Questions