Help with delete + grid in asp.net mvc

21 views
Skip to first unread message

Ale

unread,
Nov 5, 2009, 7:43:50 AM11/5/09
to mvccontrib-discuss
Hello Guys, anyone has an example of a link for deleting records using
the grid of mvccontrib?
I am doing as follows and is not working:

View:
<% = Html.Grid (Model). Columns (column => (
column.For (x => x.Codigo). Named ( "ID");
column.For (x => x.Descricao). Named ( "Description");
column.For (x => x.Orientacao). Named ( "Guideline");
column.For (x => Html.ActionLink <CFOPsController> (c =>
c.Show (x.Id), "Details")). Named ( "Detail"). DoNotEncode ();
column.For (x => Html.ActionLink <CFOPsController> (c =>
c.Edit (x.Id), "Edit")). Named ( "Issue"). DoNotEncode ();

column.For (x => Html.ActionLink <CFOPsController> (c =>
c.delete (x.Id), "Delete", new (onclick = "return confirm ( 'Are you
sure ?');")))
.
Named ( "Exclusion")
.
DoNotEncode ();
)). Empty ( "No records recorded .")%>

Controller:

[ValidateAntiForgeryToken]
[Transaction]
[AcceptVerbs (HttpVerbs.Post)]
public ActionResult Delete (int id) (
resultMessage string = "CFOP successfully deleted";
CFOP cFOPToDelete = CFOPRepository.Get (id);
if (cFOPToDelete! = null) (
CFOPRepository.Delete (cFOPToDelete);
try (
CFOPRepository.DbContext.CommitChanges ();
)
catch (
resultMessage = "There was a problem in time to
delete the CFOP." +
"Another item may depend on CFOP."
CFOPRepository.DbContext.RollbackTransaction ();
)
)
else (
resultMessage = "CFOP not found for deletion. It is
previously been excluded."
)

TempData
[ControllerEnums.GlobalViewDataProperty.PageMessage.ToString ()] =
resultMessage;
RedirectToAction return ( "Index");
)

Jeremy Skinner

unread,
Nov 5, 2009, 8:30:22 AM11/5/09
to mvccontri...@googlegroups.com
What error message are you getting?

Jeremy

2009/11/5 Ale <alessand...@gmail.com>

Alessandro Aguiar

unread,
Nov 5, 2009, 10:22:20 AM11/5/09
to mvccontri...@googlegroups.com
Hi Jeremy,

The error:

Server Error in '/' Application. 

Can not find the resource. 

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could not be removed, its name was changed, or is temporarily unavailable. Examine the URL and make sure it is spelled correctly. 

Requested Url: / CFOPs / Delete

Jeremy Skinner

unread,
Nov 5, 2009, 10:33:39 AM11/5/09
to mvccontri...@googlegroups.com
The issue here is that you've got a hyperlink pointing to your Delete action, but you've marked the Delete action as requiring an HTTP POST with [AcceptVerbs (HttpVerbs.Post)]. As a hyperlink will always issue a GET request, not a POST request, this will not work.

You'll either need to allow GET requests to your Delete action (probably not a good idea) or use a form to submit a POST to the delete action instead of using a link.

Jeremy

2009/11/5 Alessandro Aguiar <alessand...@gmail.com>

Alessandro Aguiar

unread,
Nov 5, 2009, 10:46:24 AM11/5/09
to mvccontri...@googlegroups.com
Hi Jeremy, thank you for answers. 

I commented on the attributes: 
/ / [ValidateAntiForgeryToken] 
/ / [AcceptVerbs (HttpVerbs.Post)] and it worked, but you said that this is not a good practice. Would you show me an example of how to do it the right way. 

Thank you very much, 
Alessandro

Jeremy Skinner

unread,
Nov 5, 2009, 10:56:08 AM11/5/09
to mvccontri...@googlegroups.com
Something like this should work:

<% Html.Grid(Model).Columns(column => {
        //other columns...
        column.For("Delete").Action(x => { %>
            <% using (Html.BeginForm("Delete", "MyController")) { %>
                <input type="submit" value="Delete" onclick="return confirm('Are you sure');" />
            <% } %>
        <% });
    }).Render(); %>

For this to work you'll need to ensure you have the MvcContrib.UI.Grid.ActionSyntax namespace imported. Without it the "Action" extension methods will not be available.
Reply all
Reply to author
Forward
0 new messages