Asp.net mvc 2 为什么传递给删除操作的对象为空?

Asp.net mvc 2 为什么传递给删除操作的对象为空?,asp.net-mvc-2,Asp.net Mvc 2,我有一个基本的ASP.NETMVC2应用程序。我可以添加和编辑行,但删除行不通。Delete视图在GET上获取正确的记录,但在回发时,传递的参数为空,如CategoryID=0中的所有空值。因此,找不到要从数据库中删除的对象,并引发异常。如何获得要传递给HttpPost删除操作的正确类别 以下是我在控制器中得到的: public ActionResult Delete(int id) { return View(_categoryRespository.Get(id));

我有一个基本的ASP.NETMVC2应用程序。我可以添加和编辑行,但删除行不通。Delete视图在GET上获取正确的记录,但在回发时,传递的参数为空,如CategoryID=0中的所有空值。因此,找不到要从数据库中删除的对象,并引发异常。如何获得要传递给HttpPost删除操作的正确类别

以下是我在控制器中得到的:

public ActionResult Delete(int id)
    {
        return View(_categoryRespository.Get(id));
    }

    [HttpPost]
    public ActionResult Delete(Category categoryToDelete)
    {
        try
        {
            _categoryRespository.Delete(categoryToDelete);
            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }
这是Delete视图,正如我所说,它正确地显示了GET上的数据:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MVCApp.Models.Category>" %>

删除
是否确实要删除此项?
领域
类别
节名
类型名
所容纳之物

|


您的表单实际上没有发布任何内容。您可以使用
CategoryID
添加隐藏输入,然后在存储库中创建一个静态
Delete
方法,该方法将接受CategoryID作为参数(或者通过
CategoryID
实例化一个类别,然后调用现有的
Delete
方法)

控制器

public ActionResult Delete(int id) 
{ 
    return View(_categoryRespository.Get(id)); 
} 

[HttpPost] 
public ActionResult Delete(int categoryID) 
{ 
    try 
    { 
        _categoryRespository.Delete(categoryID); 
        return RedirectToAction("Index"); 
    } 
    catch 
    { 
        return View(); 
    } 
} 
查看

<h2>Delete</h2> 

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

    <div class="display-label">CategoryID</div> 
    <div class="display-field"><%: Model.CategoryID %></div> 

    <div class="display-label">SectionName</div> 
    <div class="display-field"><%: Model.SectionName %></div> 

    <div class="display-label">CategoryName</div> 
    <div class="display-field"><%: Model.CategoryName %></div> 

    <div class="display-label">Content</div> 
    <div class="display-field"><%: Model.Content %></div> 

</fieldset> 
<% using (Html.BeginForm()) { %> 
    <p> 
        <input type="hidden" name="categoryID" value="<%: Model.CategoryID %>" />
        <input type="submit" value="Delete" /> | 
        <%: Html.ActionLink("Back to List", "Index") %> 
    </p> 
<% } %> 
删除
是否确实要删除此项?
领域
类别
节名
类别名称
所容纳之物

| 


谢谢,瑞德。这个解决方案并没有像现在这样奏效,但它让我走上了正确的道路。我不能将HttpPost Delete方法更改为只接受int,因为另一个Delete方法已经有了该签名。所以我把它改为一个int和一个字符串,并为categoryID和categoryName添加了隐藏字段。像冠军一样工作:)很好,很好地抓住了重复的签名,没有注意到这一点。
<h2>Delete</h2> 

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

    <div class="display-label">CategoryID</div> 
    <div class="display-field"><%: Model.CategoryID %></div> 

    <div class="display-label">SectionName</div> 
    <div class="display-field"><%: Model.SectionName %></div> 

    <div class="display-label">CategoryName</div> 
    <div class="display-field"><%: Model.CategoryName %></div> 

    <div class="display-label">Content</div> 
    <div class="display-field"><%: Model.Content %></div> 

</fieldset> 
<% using (Html.BeginForm()) { %> 
    <p> 
        <input type="hidden" name="categoryID" value="<%: Model.CategoryID %>" />
        <input type="submit" value="Delete" /> | 
        <%: Html.ActionLink("Back to List", "Index") %> 
    </p> 
<% } %>