多行有多个提交按钮,我应该为每个按钮制作一个表单吗? [英] Multiple rows have multiple submit buttons, should I make a form for each button?

查看:20
本文介绍了多行有多个提交按钮,我应该为每个按钮制作一个表单吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我列出了大约 20 行,每一行代表一个订单.

I am listing about 20 rows, each row represents an Order.

每一行需要有3个按钮,每次按钮点击都会执行不同的动作.

Each row needs to have 3 buttons, each button click will perform a different action.

我有 3 个操作来处​​理每个按钮发布请求,我只是不确定如何为每个按钮设置 Html 表单.

I have 3 actions to handle each button post request, I am just unsure how to setup the Html forms for each button.

<tr>
<td>
<form method="post" action="/orders/do1"><input type=button ... /></form>
<form method="post" action="/orders/do2"><input type=button ... /></form>
<form method="post" action="/orders/do3"><input type=button ... /></form>
</td>
</tr>

我应该为列表中的每一行的每个按钮创建 3 个表单吗?

Should I create 3 forms for each button, per row in my listing?

(这意味着页面上有 20 行 x 3 个表单 = 60 个表单)

(that would mean 20 rows x 3 forms = 60 forms on a page)

这样做可以"吗?(即,或者有更好的方法吗?)

Is that 'ok' to do? (i.e. or is there a better way to do this?)

推荐答案

根据您发布的代码段,我假设您并不关心实际将任何表单数据提交给您的操作 - 只是调用正确的动作(也许有些数据可以直接在动作 url 中指定,比如 id/index)

From the snippet you've posted, I'm going to presume that you don't care about actually submitting any form data to your action - just invoking the correct action (with maybe some data that can be specified directly in the action url, like an id/index)

如果是这样,那么您甚至不需要提交按钮.只是一个简单的链接(您可以将其样式设置为看起来像一个按钮,无论是您自己还是使用 jQueryUI,从而保留内容简单漂亮)

If that is the case, then you don't even need a submit button for this. Just a plain link (which you can style to look like a button, either yourself or using jQueryUI, thus keeping things simple and pretty)

如果您确实需要将表单数据提交给您的操作,只需给您的提交按钮相同的名称和不同的值并检查(或让您的模型绑定器计算出)究竟是哪个调用了该操作并让您的控制器采取相应的行动.

If you do need to submit form data to your action, just give your submit buttons the same name and different values and check (or have your model binder work out) exactly which one invoked the action and let your controller act accordingly.

请参阅此处(第二个答案,呵呵,接受的其实用了多种形式)和here(这个实际上使用了一些时髦的东西,可能有点矫枉过正,并且在生成未知数量的提交按钮,但仍然很有趣)

See here (the second answer, hehe, the accepted one actually uses multiple forms) and here (this one is actually using some funky stuff that might be a bit of an overkill, and not entirely appropriate when generating an unknown number of submit buttons, but insteresting nonetheless)

这篇关于多行有多个提交按钮,我应该为每个按钮制作一个表单吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆