【问题标题】:ASP.NET MVC 3 Delete Http PostASP.NET MVC 3 删除 Http Post
【发布时间】:2013-03-12 10:00:59
【问题描述】:

我一直在网上寻找如何执行删除 HttpPost 错过了获取,我读到发布帖子而不是获取更安全,所以这就是我想要做的。

    [HttpPost]
    public ActionResult Delete(Guid id)
    {

        var member = GetSelectedMember(id);

        _repository.DeleteEntity(member);

        TempData["message"] = String.Format("Blog {0} has been deleted!", member.Name);

        return RedirectToAction("Index");
    }

这是我的发布方法。

@using (Html.BeginForm("Delete", "Member", FormMethod.Post, new { id = item.ID }))
{
    <input type="image" src="Content/delete.png" />
}

这是我的 Razor 观点。

以下是我得到的错误

参数字典包含“GenericSaving.Controllers.MemberController”中方法“System.Web.Mvc.ActionResult Delete(System.Guid)”的不可为空类型“System.Guid”的参数“id”的空条目.可选参数必须是引用类型、可空类型或声明为可选参数。 参数名称:参数

所以,我不能中断该方法,因为它在此之前 ping 错误,我的第一个猜测是它没有将 Guid ID 传递给参数。那么根据我的表格出了什么问题?

我认为也许作为一种解决方案,我可以将它放在表单上的隐藏字段中?在 post 方法中如何收集该字段的内容?

【问题讨论】:

  • 暂停Html.BeginForm("Delete", "Member", FormMethod.Post, new { id = item.ID },看看item.ID 分配给查询字符串对象时的内容。

标签: c# asp.net-mvc-3 razor


【解决方案1】:

您为 beginForm 使用了错误的重载方法。检查HERE 是否正确使用。像这样使用:

Html.BeginForm("action","controller", new { Id = item.ID}, FormMethod.Post);

@using (Html.BeginForm("Delete", "Member", FormMethod.Post))
{
    @Html.Hidden("id", item.ID)
    <input type="image" src="Content/delete.png" />
}

【讨论】:

  • 好的,我猜你在回答我的解决方案,那么第二位在哪里,也许还有一些解释?
  • 当查询字符串已经包含值时,是否真的需要使用隐藏字段? new { id = item.ID }
  • 我会在 9 分钟内接受这个答案,如果它允许我:)
  • 那么,你真的需要声明这个表单发布了吗?并且我的表单是否发布了 enum Post 的整数?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
  • 1970-01-01
  • 2011-03-26
  • 1970-01-01
  • 2014-04-18
  • 2013-12-19
相关资源
最近更新 更多