简体   繁体   中英

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.

    [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'. 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. 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?

you are using wrong overload method for 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" />
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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