0

I have found the answer to the question "how to execute POST method using hyperlink (actionlink) instead of button" in this topic.

I have used form's submit method in this way - looks straightforward:

@foreach (var item in Model)
{
    using (Html.BeginForm("Delete", "Area", new { id = item.id }, FormMethod.Post))
    {
        <tr>
        ...
        <td>

        <a href="#" onclick="$('#sbmt').trigger('click'); return false">Delete</a>
        <input id="sbmt" type="submit" style="visibility: hidden" />

        </td>
        </tr>
    }
}

Typical declaration without button declaration

<a href="#" onclick="$(this).parents('form').submit();">Delete</a>

will not work.

The questions are:

1/ Is it possible to do the same operation but without hiding the button? I mean I don't want to use (and hide) button in my code at all.

2/ What are pros and cons of the solution listed above?

Community
  • 1
  • 1
stashko
  • 43
  • 1
  • 2
  • 9

1 Answers1

0
  1. It is possible without button.One of the way is Just simply giving id to your form like

    using (Html.BeginForm("Delete", "Area", new { id = item.id }, FormMethod.Post,new {id="frm"}))
    {
    
    }
    and then 
    <a href="javascript:$('frm').submit();" class="login-button"></a>
    

and if not jquery then

    <a href="javascript:document.getElementById("frm").submit();" class="login-button"></a>
  1. What is the problem with button I don't know. I can not see any cons except dirty coding(bad way) to submit a form.
Shashank Sood
  • 480
  • 5
  • 16