Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6810899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:15:42+00:00 2026-05-26T20:15:42+00:00

ASP.NET MVC 2 renders a link (i.e. <a> ) to delete records. It can

  • 0

ASP.NET MVC 2 renders a link (i.e. <a>) to delete records.

It can be harmful to allow delete actions through GET actions, so I want to do the delete by issuing a POST.

I’ve created the following piece of code:

<% 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 here is where I got stuck. How do I get this delete button to work?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:15:43+00:00Added an answer on May 26, 2026 at 8:15 pm

    If you want to generate the full code, you’re going about it wrong to have it return an MvcForm. You want to have it return an MvcHtmlString and construct the HTML within the method. That way you can use it as:

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

    Generating the HTML directly (note: untested, you may need suitable null checks, etc.)

    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() );
    }
    

    An alternative would be to reuse the form helpers and Response.Write, but have the method return an (empty) string, perhaps something like:

    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("");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my asp.net mvc page I create a link that renders as followed: http://localhost:3035/Formula/OverView?colorId=349405&paintCode=744&name=BRILLANT%20SILVER&formulaId=570230
I have a page which contains a html.RenderPartial, that renders an ASP.NET MVC partial
I am an ASP.Net MVC noob. I cannot get checkboxes to render correctly i.e.
In C# ASP.NET MVC application I use Link to SQL to provide data for
In MonoRail you can just CancelLayout() to not render the layout. In ASP.NET MVC,
I wanted to get your opinion about managing scripts in ASP .NET MVC. There
In the latest (RC1) release of ASP.NET MVC, how do I get Html.ActionLink to
I have an ASP.NET MVC 2 project that renders conventional strongly typed pages, but
Example is fake. ASP.NET MVC: I have a view that renders a student info.
-- Running within ASP.NET MVC alongside jQuery -- I'm using jQuery to render a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.