none
EntityState.Deleted is not working for me RRS feed

  • Question

  • I'm new to EF, so I would appreciate some assistance with this issue.  I'm using Visual Studio 2012, MVC 4, and Entity Framework 5.0.  I'm working through a sample project and the "CRU" parts of CRUD are working for me.  However, my delete is returning NULL.  My code is below:

    Controller:

    namespace MvcEntityFrameworkProject.Controllers
    {
        public class SalesOrderController : Controller
        {
            //
            // GET: /SalesOrder/
            SalesOrderModelContainer db = new SalesOrderModelContainer();

    // // GET: /SalesOrder/Delete/5 public ActionResult Delete(int id) { using (db = new SalesOrderModelContainer()) { return View(db.SalesOrders.Find(id)); } } // // POST: /SalesOrder/Delete/5 [HttpPost] public ActionResult Delete(int id, SalesOrder salesOrder) { try { using (db = new SalesOrderModelContainer()) { // TODO: Add update logic here db.Entry(salesOrder).State = System.Data.EntityState.Deleted; db.SaveChanges(); return RedirectToAction("Index"); } } catch { //return View(); return RedirectToAction("Index"); }

     

    View:

     

    @model MvcEntityFrameworkProject.Models.SalesOrder
    
    @{
        ViewBag.Title = "Delete";
    }
    
    <h2>Delete</h2>
    
    <h3>Are you sure you want to delete this?</h3>
    <fieldset>
        <legend>SalesOrder</legend>
    
        <div class="display-label">
             @Html.DisplayNameFor(model => model.SalesPersonID)
        </div>
        <div class="display-field">
            @Html.DisplayFor(model => model.SalesPersonID)
        </div>
    
        <div class="display-label">
             @Html.DisplayNameFor(model => model.OrderDate)
        </div>
        <div class="display-field">
            @Html.DisplayFor(model => model.OrderDate)
        </div>
    
        <div class="display-label">
             @Html.DisplayNameFor(model => model.Status)
        </div>
        <div class="display-field">
            @Html.DisplayFor(model => model.Status)
        </div>
    
        <div class="display-label">
             @Html.DisplayNameFor(model => model.OnlineOrderFlag)
        </div>
        <div class="display-field">
            @Html.DisplayFor(model => model.OnlineOrderFlag)
        </div>
    
        <div class="display-label">
             @Html.DisplayNameFor(model => model.AccountNumber)
        </div>
        <div class="display-field">
            @Html.DisplayFor(model => model.AccountNumber)
        </div>
    
        <div class="display-label">
             @Html.DisplayNameFor(model => model.TaxAmt)
        </div>
        <div class="display-field">
            @Html.DisplayFor(model => model.TaxAmt)
        </div>
    
        <div class="display-label">
             @Html.DisplayNameFor(model => model.TotalDue)
        </div>
        <div class="display-field">
            @Html.DisplayFor(model => model.TotalDue)
        </div>
    
        <div class="display-label">
             @Html.DisplayNameFor(model => model.SalesPersonSalesPersonID)
        </div>
        <div class="display-field">
            @Html.DisplayFor(model => model.SalesPersonSalesPersonID)
        </div>
    </fieldset>
    @using (Html.BeginForm()) {
        @Html.AntiForgeryToken()
        <p>
            <input type="submit" value="Delete" /> |
            @Html.ActionLink("Back to List", "Index")
        </p>
    }
    

    namespace MvcEntityFrameworkProject.Models
    {
        using System;
        using System.Collections.Generic;
        
        public partial class SalesOrder
        {
            public int SalesOrderID { get; set; }
            public string SalesPersonID { get; set; }
            public string OrderDate { get; set; }
            public string Status { get; set; }
            public string OnlineOrderFlag { get; set; }
            public string AccountNumber { get; set; }
            public string TaxAmt { get; set; }
            public string TotalDue { get; set; }
            public int SalesPersonSalesPersonID { get; set; }
        
            public virtual SalesPerson SalesPerson { get; set; }
        }
    }

    Please let me know if I need to post additional code, thanks.

    Wednesday, July 3, 2013 3:19 PM

Answers

  • I ended up deleting the original solution and starting over, and it works.
    • Marked as answer by gdeck Wednesday, July 3, 2013 6:48 PM
    Wednesday, July 3, 2013 6:48 PM

All replies