我一直在网上寻找如何删除 HttpPost 而错过了 get,我读到,做 post 而不是 Get 更安全,所以这就是我想做的。
[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 View 。
下面是我得到的错误
The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Guid' for method 'System.Web.Mvc.ActionResult Delete(System.Guid)' in 'GenericSaving.Controllers.MemberController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters
因此,我无法中断该方法,因为它在此之前会 ping 错误,我的第一个猜测是它没有将 Guid ID 传递给参数。那么根据我的表格,出了什么问题?
我想也许作为解决方案我可以将它放在表单上的隐藏字段中?我如何在post方法中收集该字段的内容?
最佳答案
您对 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" />
}
关于c# - ASP.NET MVC 3 删除 Http Post,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15358134/