为什么删除未能删除记录?

时间:2015-12-05 01:22:57

标签: asp.net-mvc-4

我对删除操作有以下操作:

[HttpPost]
public ActionResult Delete(int id, EmployeeDeleteViewModel collection)
{
    try
    {
        if (ModelState.IsValid)
        {
            Employee e = new Employee 
            { 
                EmpId = collection.EmpID, 
                FirstName = collection.FirstName, 
                LastName = collection.LastName, 
                DepartmentId = collection.DepartmentID 
            };

            db.Employees.Remove(e);
            db.SaveChanges();
            return RedirectToAction("Index", new { id = id });
        }

        // TODO: Add update logic here

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

删除视图是:

@model VirtualCampus2.Models.EmployeeDeleteViewModel

@{
    ViewBag.Title = "Delete";
}

<h2>Delete</h2>

<h3>Are you sure you want to delete this?</h3>

<fieldset>
    <legend>EmployeeDeleteViewModel</legend>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.EmpID)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.EmpID)
    </div>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.FirstName)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.FirstName)
    </div>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.LastName)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.LastName)
    </div>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.DepartmentName)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.DepartmentName)
    </div>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.DepartmentID)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.DepartmentID)
    </div>
</fieldset>
@using (Html.BeginForm())
{
    <p>
        <input type="submit" value="Delete" /> |
        @Html.ActionLink("Back to List", "Index")
    </p>
}

当我在删除视图上单击“删除”时,会发生以下错误:

  1. 到action方法的第二个参数&#34; collection&#34;,它会在其属性中发送一个零和null的集合
  2. 不会删除记录
  3. 这是video that shows the problem

    为什么会发生这种情况,我该如何解决这个问题?

    史蒂夫: 我通过创建单独的视图模型和删除操作来进行更改:

    视图模型:

    public class EmpDeleteCommitViewModel
    {
        public int EmpID { get; set; }
    }
    

    操作删除方法:

            [HttpGet]//this works fine, gets the record to show on view
            public ActionResult Delete(int id)
            {
                var empList = db.Employees.ToList();
    
                var employee = empList.Where(e => e.EmpId == id).Select(e => new EmployeeDeleteViewModel 
                {
                    EmpID=e.EmpId, 
                    FirstName= e.FirstName, 
                    LastName=e.LastName, 
                    DepartmentID=e.DepartmentId,
                    DepartmentName=e.Department.Name
                }).FirstOrDefault();
    
                return View(employee);
            }        
    
    
    
    [HttpPost] //BUT THIS DOES NOT WORK!, evm EmpID does not contain id value
    public ActionResult Delete(EmpDeleteCommitViewModel evm)
            {
                try
                {
                    var employee = db.Employees.Where(e => e.EmpId == evm.EmpID).FirstOrDefault();
                    db.Employees.Remove(employee);
                    db.SaveChanges();
                    return RedirectToAction("Index", new { id = evm.EmpID });
    
                }
                catch
                {
                    return View();
                }
            }
    

1 个答案:

答案 0 :(得分:2)

表单标记之间没有表单控件(<input>),因此在提交表单时没有任何内容可以回发。您所做的只是生成一些文本来显示属性值。

您无需在方法中包含EmployeeDeleteViewModel collection参数。假设您使用正确的路由,您的int id参数将与员工的id绑定,因此您需要根据id和数据库从数据库中获取原始数据模型。然后删除它。