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>
<%}%>
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.
There are a total of 13 overloaded ways to use and implement @Html. BeginForm.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With