Creating MVC Delete Button Extension - How to Extend MVC Html Helper?

ASP.NET MVC 2 provides a link (i.e. <a>) to delete entries.

It can be harmful to allow delete actions using GET actions, so I want to do a delete by sending a POST.

I created the following code snippet:

<% using (Html.BeginForm("Delete", "Boodschap", new { id = item.BoodschapID }))
    { %>
    <button>Delete</button>
<% } %>

Now I would like to add this code to the HTML helper as an extension method:

public static MvcForm DeleteButton(this HtmlHelper helper, string name, 
    string actionName, string controllerName, string routeValues)
{
    MvcForm form = helper.BeginForm(actionName, controllerName, routeValues);
    return form;
}

Now this is where I am stuck. How can I make this delete button work?

+5
source share
2 answers

, , MvcForm. , MvcHtmlString HTML . :

@Html.DeleteButton( "Delete", "Boodschap", new { id = item.BoodschapID } );

HTML ( : , ..)

public static MvcHtmlString DeleteButton( this HtmlHelper helper, string name, 
    string actionName, object htmlAttributes )
{
     return DeleteButton( helper, name, actionName, null, null, htmlAttributes );
}

public static MvcHtmlString DeleteButton( this HtmlHelper helper, string name, 
    string actionName, string controllerName, object routeValues, 
    object htmlAttributes )
{
     var buttonBuilder = new TagBuilder("button");
     buttonBuilder.SetInnerText( name );

     var formBuilder = new TagBuilder("form");
     var urlHelper = new UrlHelper( helper.ViewContext.RequestContext );
     formBuilder.Attributes.Add( "action", urlHelper.Action( 
         actionName, controllerName, routeValues ) )
     formBuilder.Attributes.Add( "method", FormMethod.Post );
     formBuilder.MergeAttributes( new RouteValueDictionary( htmlAttributes ) );
     formBuilder.InnerHtml = buttonBuilder.ToString();

     return new MvcHtmlString( formBuilder.ToString() );
}

Response.Write, () , , - :

public static MvcHtmlString DeleteButton(this HtmlHelper helper, string name, string actionName, object routeValues)
{
    return DeleteButton(helper, name, actionName, null, routeValues, null);
}

public static MvcHtmlString DeleteButton(this HtmlHelper helper, string name, string actionName, string controllerName, object routeValues, object htmlAttributes)
{
    using (helper.BeginForm(actionName, controllerName, routeValues, FormMethod.Post, htmlAttributes))
    {
        var response = helper.ViewContext.HttpContext.Response;
        var builder = new TagBuilder("button");
        builder.SetInnerText(name);
        response.Write(builder.ToString(TagRenderMode.Normal));
    }
    return MvcHtmlString.Create("");
}
+4

, <form> , AJAX-y.

, jQuery, <a>, HTTP POST ?

$document.ready(function () {
    // "deleteLink is a class that identifies links that
    // are used for deleting, you might have some other mechanism
    $("a .deleteLink").click(function () { 
        $.post('post url', function(data) {
            // Do something with the data returned
        });    
    });
});

, HTML , <form> , , , , SEO .

+1

All Articles