Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7719311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:23:03+00:00 2026-06-01T03:23:03+00:00

My delete method is not working, here is what is happening at the moment.

  • 0

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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T03:23:05+00:00Added an answer on June 1, 2026 at 3:23 am

    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();
                }
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently ran into this problem: Rails 3 link_to (:method => :delete) not working
I have a question regarding providing a custom delete-method to boost::shared_ptr constructor. For example,
I have been having an issue with the $this->delete() method that deletes a record
I have a method that deletes a row form a database using the sql
In the commitEditingStyle tableView method, the .row property is not working on the NSIndexPath.
Have been working on a Tower Defense game for iOS for some time now.
I'm using Entity Framework Code First and whilst I have working code, I'm having
I'm looking to try to make an efficient delete method to work on a
I writing a simple method which should delete an entity from the database if
I have this recursive method which deletes empty folders: private void DeleteEmpty(DirectoryInfo directory) {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.