Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC Form Submit with Link instead of Button

I would like to have a simple login form (or any form for that matter), and be able to post it, not with a button, but with a link.

So no input, but a.

I've seen plenty of solutions for this type of question, and I've tried lots of different options, and every single time, nothing happens.

I've stripped all the JQuery from what I have, so this is the "base" situation: what's missing to make it submit the form?

<% using (Html.BeginForm("Login", "Account", FormMethod.Post, new { id = "loginForm" }))
  { %>
       <%= Html.TextBoxFor(x => x.UserName)%><br/>
       <%= Html.TextBoxFor(x => x.Password)%><br/>
       <a href="#" id="submit-link" class="button">Log In</a>
<%}%>
like image 470
Bertvan Avatar asked Jul 16 '10 14:07

Bertvan


People also ask

How do I submit a form by Link?

In the body tag, created an HTML form and specify the id, method, and action of the form. In the form, specify an anchor tag with an event onclick. Create a function for JavaScript that will get executed when the link is clicked. When we click on the link, the function submitForm() will get executed.

How many ways are there to submit a form in asp net MVC?

There are a total of 13 overloaded ways to use and implement @Html. BeginForm.

Can an ActionLink be a POST?

ActionLink is rendered as an HTML Anchor Tag (HyperLink) and hence it produces a GET request to the Controller's Action method which cannot be used to submit (post) Form in ASP.Net MVC 5 Razor.

What is FormMethod post in MVC?

FormMethod – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST. There are three TextBox fields created for capturing values for PersonId, Name and City using the Html. TextBoxFor method. While for capturing the Gender value, a DropDownList with three options is created using the Html.


1 Answers

The only way to make this happen is using javascript:

<form id="fooForm" action="/foo" method="post">
    <a href="#" id="submit_link" class="button">Log In</a>
</form>

and in javascript:

$(function() {
    $('a#submit_link').click(function() {
        $('form#fooForm').submit();
    });
});

Although I would suggest you using a submit button:

<button type="submit">
    <a href="#" id="submit_link" class="button">Log In</a>
</button>

And try to style it to look as an anchor. My suggestion is to always use submit buttons to submit forms. This is more semantically correct, you will also find that things like pressing the Enter key while editing a text field will submit the form which is kind of native. So do what's semantically correct and do the styling in CSS.

like image 93
Darin Dimitrov Avatar answered Nov 02 '22 23:11

Darin Dimitrov