Asp.net mvc 3 删除方法在MVC 3中不起作用

Asp.net mvc 3 删除方法在MVC 3中不起作用,asp.net-mvc-3,view,controller,Asp.net Mvc 3,View,Controller,我的删除方法不起作用,下面是目前正在发生的情况。我使用EF创建了一个CRUD来与现有数据库交互。现在我的编辑方法和索引列表都很好,但是删除方法仍然给我带来问题。当我转到特定记录的“删除”按钮时,它会显示该记录的“删除”视图,并询问我是否确实要删除该记录。然后我点击submit,数据从视图中消失,但不会像它应该做的那样将我重定向到索引页面。当我回击列表时,它会将我带回索引视图,但记录仍在表中。数据从视图中消失,而不是从数据库中消失,这对我来说似乎很奇怪 从其他示例中我可以看出,我的代码看起来是正确

我的删除方法不起作用,下面是目前正在发生的情况。我使用EF创建了一个CRUD来与现有数据库交互。现在我的编辑方法和索引列表都很好,但是删除方法仍然给我带来问题。当我转到特定记录的“删除”按钮时,它会显示该记录的“删除”视图,并询问我是否确实要删除该记录。然后我点击submit,数据从视图中消失,但不会像它应该做的那样将我重定向到索引页面。当我回击列表时,它会将我带回索引视图,但记录仍在表中。数据从视图中消失,而不是从数据库中消失,这对我来说似乎很奇怪

从其他示例中我可以看出,我的代码看起来是正确的,但它不可能是正确的,因为它不工作!我不确定这个错误可能来自哪里,所以我在下面发布了我的控制器、删除类和索引类的代码

遥控器 删除类 索引类
@model IEnumerable
@{
ViewBag.Title=“Index”;
}
指数

@ActionLink(“新建”、“创建”)

帕 重大项目 投资领域 @foreach(模型中的var项目){ @DisplayFor(modeleItem=>item.PA) @DisplayFor(modelItem=>item.MAJOR\u程序) @DisplayFor(modelItem=>item.INVESTMENT\u区域) @ActionLink(“编辑”,“编辑”,新的{id=item.PA})| @ActionLink(“Delete”,“Delete”,new{id=item.PA}) }
所以我想出了我自己的问题,尽管此时有27人看了我的问题,甚至没有任何评论(不仅仅是说)。发生这种情况的原因是删除一行后,主键将被删除并返回空值。主键不能有空值,因此这导致我的代码在这里抛出错误

[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();
        }
    }
[HttpPost]
公共操作结果删除(字符串id、iamp\U映射iamp)
{
尝试
{
使用(var db=new paenties())
{
db.Entry(IAMP).State=EntityState.Deleted;
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();
            }
        }
    }
}
   @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>
[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();
        }
    }
[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();
            }
        }