简体   繁体   English

ASP.NET MVC 3删除Http发布

[英]ASP.NET MVC 3 Delete Http Post

I've been looking around the web for how to do just a delete HttpPost missing out the get, i've read that it is safer to do a post instead of a Get, so that is what i want to do. 我一直在网上寻找如何删除丢失的getHttp HttpPost,我已经读到,发布而不是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");
    }

That is my Post Method. 那是我的张贴方法。

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

That is my Razor view. 那是我的剃刀观点。

Below is the error i get 以下是我得到的错误

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'. 参数字典包含“ GenericSaving.Controllers.MemberController”中方法“ System.Web.Mvc.ActionResult Delete(System.Guid)”的非空类型“ System.Guid”的参数“ id”的空条目。 An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. 可选参数必须是引用类型,可为空的类型,或者必须声明为可选参数。 Parameter name: parameters 参数名称:参数

So, I can't break on the method because it pings the error before then, my first guess is that it isn't passing the Guid ID to the parameter. 因此,我无法中断该方法,因为它会在此之前ping错误,我的第一个猜测是它没有将Guid ID传递给该参数。 So based on my form what is going wrong? 那么根据我的表格,出了什么问题?

I've thought that maybe as a solution i could put it in a hidden field on the form? 我以为也许可以作为解决方案将其放在表单的隐藏字段中? How would I collect the contents of this field in the post method? 我将如何在post方法中收集此字段的内容?

you are using wrong overload method for beginForm. 您为beginForm使用了错误的重载方法。 Check HERE for correct usage. 这里检查正确的用法。 Use like this: 像这样使用:

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

OR 要么

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM