Goldentp
Goldentp

Reputation: 197

Delete Method not working in MVC 3

My delete method is not working, here is what is happening at the moment. Using EF I created a CRUD to interact with an existing DB. Right now I have the Edit method working fine as well as the index list but the Delete method is still giving me problems. When I go to the delete button for a specific record it brings up the delete view for that record and asks if I am sure I want to delete the record. I then hit submit and the data disappears from the view but does not redirect me to the index page like it should be doing. When I hit back to list it takes me back to the index view but the record is still in the table. It seems strange to me that the data disappears from the view but not the DB.

From what I can tell from other examples I have seen my code looks correct but it can't be because it's not working! I'm unsure of where this error could be coming from so below I have posted my code for my controller, Delete class, and index class.

PACONTROLLLER

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DBFirstMVC.Models;
using System.Data;

namespace DBFirstMVC.Controllers
{
    public class PaController : Controller
    {
        PaEntities db = new PaEntities();
        //
        // GET: /Pa/

        public ActionResult Index()
        {
            using (var db = new PaEntities())
            {
                return View(db.iamp_mapping.ToList());
            }
        }

        //
        // GET: /Pa/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Pa/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Pa/Create

        [HttpPost]
        public ActionResult Create(iamp_mapping IAMP)
        {
            try
            {
                using (var db = new PaEntities())
                {
                    db.iamp_mapping.Add(IAMP);
                    db.SaveChanges();
                }

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Pa/Edit/5

        public ActionResult Edit(string id)
        {

            using (var db = new PaEntities())

            {

                return View(db.iamp_mapping.Find(id));
            }
        }

        //
        // POST: /Pa/Edit/5

        [HttpPost]
        public ActionResult Edit(string id, iamp_mapping IAMP)
        {
            try
            {
                using (var db = new PaEntities())
                {
                    db.Entry(IAMP).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("Pa");
                }
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Pa/Delete/5

        public ActionResult Delete(string id)
        {
            using (var db = new PaEntities())
            {

                return View(db.iamp_mapping.Find(id));
            }
        }

        //
        // POST: /Pa/Delete/5

        [HttpPost]
        public ActionResult Delete(string id, iamp_mapping IAMP)
        {
            try
            {
                using (var db = new PaEntities())
                {
                    db.Entry(IAMP).State = EntityState.Deleted;
                    db.SaveChanges();
                    return RedirectToAction("Pa");
                }

            }
            catch
            {
                return View();
            }
        }
    }
}

DELETE CLASS

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DBFirstMVC.Models;
using System.Data;

namespace DBFirstMVC.Controllers
{
    public class PaController : Controller
    {
        PaEntities db = new PaEntities();
        //
        // GET: /Pa/

        public ActionResult Index()
        {
            using (var db = new PaEntities())
            {
                return View(db.iamp_mapping.ToList());

        }
    }

    //
    // GET: /Pa/Details/5

    public ActionResult Details(int id)
    {
        return View();
    }

    //
    // GET: /Pa/Create

    public ActionResult Create()
    {
        return View();
    } 

    //
    // POST: /Pa/Create

    [HttpPost]
    public ActionResult Create(iamp_mapping IAMP)
    {
        try
        {
            using (var db = new PaEntities())
            {
                db.iamp_mapping.Add(IAMP);
                db.SaveChanges();
            }

            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }

    //
    // GET: /Pa/Edit/5

    public ActionResult Edit(string id)
    {

        using (var db = new PaEntities())

        {

            return View(db.iamp_mapping.Find(id));
        }
    }

    //
    // POST: /Pa/Edit/5

    [HttpPost]
    public ActionResult Edit(string id, iamp_mapping IAMP)
    {
        try
        {
            using (var db = new PaEntities())
            {
                db.Entry(IAMP).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Pa");
            }
        }
        catch
        {
            return View();
        }
    }

    //
    // GET: /Pa/Delete/5

    public ActionResult Delete(string id)
    {
        using (var db = new PaEntities())
        {

            return View(db.iamp_mapping.Find(id));
        }
    }

    //
    // POST: /Pa/Delete/5

    [HttpPost]
    public ActionResult Delete(string id, iamp_mapping IAMP)
    {
        try
        {
            using (var db = new PaEntities())
            {
                db.Entry(IAMP).State = EntityState.Deleted;
                db.SaveChanges();
                return RedirectToAction("Pa");
            }

        }
        catch
        {
            return View();
            }
        }
    }
}

INDEX Class

   @model IEnumerable<DBFirstMVC.Models.iamp_mapping>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            PA
        </th>
        <th>
            MAJOR PROGRAM
        </th>
        <th>
            INVESTMENT AREA
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.PA)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.MAJOR_PROGRAM)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.INVESTMENT_AREA)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.PA }) |

            @Html.ActionLink("Delete", "Delete", new { id=item.PA })
        </td>
    </tr>
}

</table>

Upvotes: 0

Views: 2476

Answers (1)

Goldentp
Goldentp

Reputation: 197

So I figured out my own question even though at this time 27 people have viewed my question at this time without even a comment(not bitter just sayin'). The reason that this is happening is after you delete a row the primary key gets deleted and returns a null value. Primary keys cannot have null values so this was causing my code to throw an error here.

[HttpPost]
        public ActionResult Delete(string id, iamp_mapping IAMP)
        {
            try
            {
                using (var db = new PaEntities())
                {
                    db.Entry(IAMP).State = EntityState.Deleted; <---------
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }


        }
        catch (Exception e)
        {
            throw (e);
            //return View();
        }
    }

I fixed this by changing the primary key variable the program uses to bring up the id like this.

[HttpPost]
        public ActionResult Delete(string id, iamp_mapping IAMP)
        {
            try
            {
                using (var db = new PaEntities())
                {
                    var vIAMP = db.iamp_mapping.Find(id); <---------------
                    db.Entry(vIAMP).State = EntityState.Deleted;
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }

            }
            catch (Exception e)
            {
                throw (e);
                //return View();
            }
        }

Upvotes: 2

Related Questions