开发者

Using a link instead of a button in beginform

How do I use a link instead of a button to submit a form in beginform

开发者_如何学运维

This is what I have, but i dont wanna use a button

           <% using (Html.BeginForm())
             { %>
             - <%: role %>
             <%: Html.Hidden("Username", user) %>
             <%: Html.Hidden("RoleToDelete", role) %>
             <button type="submit">X</button>
          <% } %> 

Thanks


Add some javascript to the link:

<a href="#" onclick="document.nameofyourform.submit();">X</a>

You'd need some method of identifying your form so you can call its related object's submit method, such that your generated html looks something like:

<form id="myform" name="nameofyourform" action="..." method="...">
...
</form>

The id would let you do document.getElementById('myform'.submit(), and the name will work as in the <a> example above.

Of course, this all depends on Javascript. If you're targetting older/js-deprived browsers, the only way you'll be able to submit the form is via an actual submit button


Since you're using MVC i assume you will also use JQuery:

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

Drawbacks:

  • doesn't work when pressing enter (workaround: keep the submit button but position it offscreen)

  • doesn't work when JS disabled on browser


You need to name your form first with BeginForm, then you can refernce it in the link's onclick event.

<% using (Html.BeginForm("ActionName", "ControllerName", FormMethod.Post, new { id = "deletionform" }))
{ %>
- <%: role %>
<%: Html.Hidden("Username", user) %>
<%: Html.Hidden("RoleToDelete", role) %>
<a onclick="deletionform.submit();">X</button>
<% } %> 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜