ASP.NET MVC Form Submit with Link instead of Button

Viewed 55065

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>
<%}%>
7 Answers

Try inline JavaScript

   <a href="javascript:document.forms[0].submit()">
      Submit
    </a>

In razor syntax and to add on Amer's suggestion; here's successful implementation I used:

Add the id of your form in the BeginForm method in the htmlAttributes.

Then use the inline JavaScript HREF to reference the form by it's id and submit it.

using (Html.BeginForm(actionName: "LogOff", controllerName:"Account", routeValues: new { Area = "" },method: FormMethod.Post, htmlAttributes: new {id = "logoutForm", @class = "navbar-right" }))
{
@Html.AntiForgeryToken()
<a href="javascript:document.getElementById('logoutForm').submit()">Log off</a>
}
Related