I have been searching around for a bit now. I have tried a couple solutions involving catching the OptimisticConcurrency and adding:
"@Html.HiddenFor(model => model.OrganizationID)"
to my delete page. My index (list), create, and edit pages work like a charm. However, when I go to delete a row it gives me the error below:
DbUpdateConcurrencyException
Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries.
I have followed a tutorial to build a Database First application. Currently, I am just bringing in data from my Organizations table until I can get it working smoothly. My organization model looks like this (which was auto-generated from "Add Code Generation Item"):
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace VAGTC.Models
{
using System;
using System.Collections.Generic;
public partial class Organization
{
public Organization()
{
this.Contact_Title = new HashSet<Contact_Title>();
this.Organization_Address = new HashSet<Organization_Address>();
this.Organization_Business_Type = new HashSet<Organization_Business_Type>();
this.Organization_Country = new HashSet<Organization_Country>();
this.Organization_Email = new HashSet<Organization_Email>();
this.Organization_Membership = new HashSet<Organization_Membership>();
this.Organization_Notes = new HashSet<Organization_Notes>();
this.Organization_Phone = new HashSet<Organization_Phone>();
this.Organization_Website = new HashSet<Organization_Website>();
this.Contacts = new HashSet<Contact>();
this.Organization_Industry_Code = new HashSet<Organization_Industry_Code>();
}
public int OrganizationID { get; set; }
public string Name { get; set; }
public virtual ICollection<Contact_Title> Contact_Title { get; set; }
public virtual ICollection<Organization_Address> Organization_Address { get; set; }
public virtual ICollection<Organization_Business_Type> Organization_Business_Type { get; set; }
public virtual ICollection<Organization_Country> Organization_Country { get; set; }
public virtual ICollection<Organization_Email> Organization_Email { get; set; }
public virtual ICollection<Organization_Membership> Organization_Membership { get; set; }
public virtual ICollection<Organization_Notes> Organization_Notes { get; set; }
public virtual ICollection<Organization_Phone> Organization_Phone { get; set; }
public virtual ICollection<Organization_Website> Organization_Website { get; set; }
public virtual ICollection<Contact> Contacts { get; set; }
public virtual ICollection<Organization_Industry_Code> Organization_Industry_Code { get; set; }
}
}
This is the Delete ActionResult in my Organization controller:
//
// GET: /Organization/Delete/5
public ActionResult Delete(int id)
{
using (var db = new VAGTCEntities())
{
return View(db.Organizations.Find(id));
}
}
//
// POST: /Organization/Delete/5
[HttpPost]
public ActionResult Delete(int id, Organization organization)
{
try
{
// TODO: Add delete logic here
using (var db = new VAGTCEntities())
{
db.Entry(organization).State = System.Data.EntityState.Deleted;
db.SaveChanges();
}
return RedirectToAction("Index");
}
catch
{
return View();
}
}
and on my Index page it declares the primary key:
@Html.ActionLink("Delete", "Delete", new { id=item.OrganizationID })
Out of curiosity I decided to try adding
"@Html.HiddenFor(model => model.OrganizationID)"
to the bottom, under BeginForm, instead of at the top of the page in fieldset and under legend tags:
@using (Html.BeginForm()) {
<p>
@Html.HiddenFor(model => model.OrganizationID)
<input type="submit" value="Delete" /> |
@Html.ActionLink("Back to List", "Index")
</p>
}
Low and behold - it worked. I still want to post this! Perhaps someone else will find it and it will help them. Although I don't know 100% why it is - could someone shed some light on the matter?
when you use the from helper
@using (Html.BeginForm()) {
it spits the following output
<form>
</form>
if you want the hidden field to be posted you need to get it inside the form otherwise it will not be posted, that was the reason the OrganizationID
was 0
when you put the hidden field outside the form ...