I am running into an issue when attempting to delete a ContactGroup. The initial GET for the Delete returns the proper ContactGroup. But the POST is not receiving same ContactGroup from MVC. While debugging the POST action, I see that a default ContactGroup
with an Id of 0 and a GroupName of null is being passed to the POST action by MVC.
[AcceptVerbs(HttpVerbs.Post)] public ActionResult Delete(ContactGroup group) { if (service.DeleteContactGroup(group)) return RedirectToAction("Index");
asp:Content
ID="Content1"
ContentPlaceHolderID="TitleContent"
runat="server">
Delete
</asp:Content>
<asp:Content
ID="Content2"
ContentPlaceHolderID="MainContent"
runat="server">
<h2>Delete</h2>
<p>
Are you sure that you want to delete the group
<%= Model.GroupName %> and all contacts associated with the group?
</p>
<% using (Html.BeginForm(new
{ id = Model.ContactGroupId })) {%>
<p>
<input
type="submit"
value="Delete"
/>
</p>
<% } %>
</asp:Content>
Could someone let me know why this is happening, and what I need to do to fix it?
Try changing the second one to take an int instead, lookss like that's all you're passing up. Unless you have a modelbinder in there, you won't get a proper object conversion.
Paul
Mark as answered if this helps, thanks.
Help those who have helped you... remember to "Mark as Answered"
Yeah. Most other tutorials do pass the integer into the POST Delete action. However, the MVC Contact Manager Tutorial from this site did not. I was trying to find out how to accomplish the task in the same way as the Tutorial. Perhaps this Tutorial was
created before RTM 1.0.
Here is the Tutorial Code. In my code, I renamed the Group object and some of its members for sake of specificity. The Create, and Edit actions are working fine with the renamed objects. Only the Delete action is causing problems.
GET Action
public
ActionResult Delete(int id)
{
return View("Delete", _service.GetGroup(id));
}
POST Action
[AcceptVerbs(HttpVerbs.Post)]
public
ActionResult Delete(Group groupToDelete)
{
if (_service.DeleteGroup(groupToDelete))
return RedirectToAction("Index");
return View("Delete", _service.GetGroup(groupToDelete.Id));
}
I am interested in creating a delete action with a POST using an object instead of an integer. If an id is used for the GET, then an id can only be used for the POST if an additional unneeded and unused parameter is placed into the POST signature. Without
this dummy parameter, a duplicate signature error is thrown.
Can a delete POST action be performed in the current version of MVC by passing in an object instead of an integer?
geomar
Member
169 Points
97 Posts
ContactManager Tutorial #6: Delete Post Action For ContactGroup
Apr 14, 2009 09:47 PM|LINK
I am running into an issue when attempting to delete a ContactGroup. The initial GET for the Delete returns the proper ContactGroup. But the POST is not receiving same ContactGroup from MVC. While debugging the POST action, I see that a default ContactGroup with an Id of 0 and a GroupName of null is being passed to the POST action by MVC.
GET Action (OK)
public ActionResult Delete(int id) {
return View("Delete", service.GetContactGroup(id));
}
POST Action (Not OK)
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Delete(ContactGroup group) {
if (service.DeleteContactGroup(group))
return RedirectToAction("Index");
return View("Delete", service.GetContactGroup(group.ContactGroupId));
}
GET VIEW
<%
@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<ContactManager.Models.ContactGroup>" %><
asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">Delete
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Delete</h2>
<p>
Are you sure that you want to delete the group
<%= Model.GroupName %> and all contacts associated with the group?
</p>
<% using (Html.BeginForm(new { id = Model.ContactGroupId })) {%>
<p>
<input type="submit" value="Delete" />
</p>
<% } %>
</asp:Content>
Could someone let me know why this is happening, and what I need to do to fix it?
Thanks,
Mark
paul.vencill
Contributor
6716 Points
1358 Posts
Re: ContactManager Tutorial #6: Delete Post Action For ContactGroup
Apr 15, 2009 04:19 AM|LINK
Try changing the second one to take an int instead, lookss like that's all you're passing up. Unless you have a modelbinder in there, you won't get a proper object conversion.
Paul
Mark as answered if this helps, thanks.
geomar
Member
169 Points
97 Posts
Re: ContactManager Tutorial #6: Delete Post Action For ContactGroup
Apr 15, 2009 12:34 PM|LINK
Yeah. Most other tutorials do pass the integer into the POST Delete action. However, the MVC Contact Manager Tutorial from this site did not. I was trying to find out how to accomplish the task in the same way as the Tutorial. Perhaps this Tutorial was created before RTM 1.0.
Here is the Tutorial Code. In my code, I renamed the Group object and some of its members for sake of specificity. The Create, and Edit actions are working fine with the renamed objects. Only the Delete action is causing problems.
GET Action
public ActionResult Delete(int id){
return View("Delete", _service.GetGroup(id));
}
POST Action
[AcceptVerbs(HttpVerbs.Post)]public ActionResult Delete(Group groupToDelete)
{
if (_service.DeleteGroup(groupToDelete))
return RedirectToAction("Index"); return View("Delete", _service.GetGroup(groupToDelete.Id));
}
GET View
<%
@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<ContactManager.Models.Group>" %> <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server"><title>Delete</title>
</asp:Content>
<
asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"><h2>Delete</h2>
<p>
Are you sure that you want to delete the group
<%= Model.Name %> and all contacts associated with the group?
</p>
<%
using (Html.BeginForm(new { Id = Model.Id })) { %><p>
<input type="submit" value="Delete" />
</p>
<% } %>
</asp:Content>
Thanks.
geomar
Member
169 Points
97 Posts
Re: ContactManager Tutorial #6: Delete Post Action For ContactGroup
Apr 15, 2009 07:30 PM|LINK
I am interested in creating a delete action with a POST using an object instead of an integer. If an id is used for the GET, then an id can only be used for the POST if an additional unneeded and unused parameter is placed into the POST signature. Without this dummy parameter, a duplicate signature error is thrown.
Can a delete POST action be performed in the current version of MVC by passing in an object instead of an integer?
Thanks