Jack88PD
Jack88PD

Reputation: 615

Getting message “Store update, insert, or delete statement affected an unexpected number of rows (0)” ?

Another big problem, here.

I have the model:

public class Lead
    {
        [System.ComponentModel.DataAnnotations.Column("le_codlead")]
        public int LeadID {get; set; }

        [System.ComponentModel.DataAnnotations.Column("le_descr1")]
        [Required(ErrorMessage="Inserire Nome del Lead")]
        [Display(Name="Nominativo Lead")]
        public string Name1 {get; set;}

        [System.ComponentModel.DataAnnotations.Column("le_descr2")]
        [Display(Name = "Nominativo secondario")]
        public string Name2 { get; set; }
        ...
        ...
        ...
    }

Controller (for the Create Method):

[HttpPost]
        public ActionResult Create(Lead lead)
        {
            if (ModelState.IsValid)
            {
                db.Leads.Add(lead);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }
            return View(lead);
        }

and this is the View:

@model CRMArcadia.Models.Lead

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true, "ATTENZIONE! Compilare tutti i campi obbligatori")
    <fieldset>
        <legend>Lead</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Name1)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Name1)
            @Html.ValidationMessageFor(model => model.Name1)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Name2)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Name2)
            @Html.ValidationMessageFor(model => model.Name2)
        </div>
        ...
        ...
        ...
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset> }

But when I try to insert a new Lead it throws the exception as in the Post Title: Entity Framework: “Store update, insert, or delete statement affected an unexpected number of rows (0).” I read on the web it is caused by the ID key (LeadID, in this case) not updating: they suggest to insert an hiddenfor attribute in the view for the ID key, and so I did:

    @Html.HiddenFor(model => model.LeadID)

But with this one at the pressure of the Create button nothing happens.

Does someone has a solution (or a workaround ;) ) at this problem, please?

Thanks.

Upvotes: 0

Views: 717

Answers (1)

Yusubov
Yusubov

Reputation: 5843

Do you have some extra layer to communicate with EF ? what is your entity context?

For Update scenario:

If you are using same action for Create/Edit then just place your @Html.HiddenFor(model => model.LeadID) - hidden field between blocks @using (Html.BeginForm()) { .... </fieldset> }.

If it does not help, look at this article MSDN - Working with Objects

Upvotes: 1

Related Questions