且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

多行有多个提交按钮,我应该为每个按钮制作一个表单吗?

更新时间:2023-11-22 16:18:22

根据您发布的代码段,我假设您并不关心实际将任何表单数据提交给您的操作 - 只是调用正确的动作(也许有些数据可以直接在动作 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)