asp.net-mvc 向表单提交添加附加参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13162819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding additional parameter to form submit
提问by Sachin Kainth
I have a form declaration in my Razor view
我的 Razor 视图中有一个表单声明
<form method="post" action="/Lot/LotList?auctionEventId=@auctionEventId" id="filterForm">
(Incidentally, I chose to write it out like this rather than use Html.BeginFrombecause I needed to give it an id and didn't know how to do that with Html.BeginFrom- but that is not the issue here)
(顺便说一句,我选择这样写而不是使用,Html.BeginFrom因为我需要给它一个 id 并且不知道如何使用Html.BeginFrom- 但这不是这里的问题)
Outside of this form I have a button which submits this form (there is also a submit button in the form)
在此表单之外,我有一个提交此表单的按钮(表单中还有一个提交按钮)
<input type="button" onclick="$('#filterForm').submit();" value="Show all" />
Now, the issue is that if this button is used to submit the form I want the action in the form to change to
现在,问题是,如果使用此按钮提交表单,我希望表单中的操作更改为
action="/Lot/LotList?auctionEventId=@auctionEventId&showAll=true"
How do I alter the action and pass this additional parameter? Is there a totally better way of doing all this?
如何更改操作并传递此附加参数?有没有更好的方法来做这一切?
回答by Jamiec
Appending query string parameters onto the form action, and trying to change that at runtime is tricky (but not impossible). Far easier is to use hidden fields:
将查询字符串参数附加到表单操作上,并尝试在运行时更改它是很棘手的(但并非不可能)。使用隐藏字段要容易得多:
<form method="post" action="/Lot/LotList" id="filterForm">
<input type="hidden" name="auctionEventId" value="@auctionEventId" />
...
So now all you have to do is add another hidden field for "showAll"
所以现在您要做的就是为“showAll”添加另一个隐藏字段
<form method="post" action="/Lot/LotList" id="filterForm">
<input type="hidden" name="auctionEventId" value="@auctionEventId" />
<input type="hidden" name="showAll" value="false" id="showAllField" />
...
And just hook up a jquery event on your showAll button:
只需在 showAll 按钮上连接一个 jquery 事件:
<input id="showAllButton" type="button"/>
jQuery:
jQuery:
$('#showAllButton').click(function(){
$('#showAllField').val("true");
$('#filterForm').submit();
});
回答by yardpenalty.com
I know you said this is not the issue but you can add attributes to Html.BeginFormlike this:
我知道你说这不是问题,但你可以添加这样的属性Html.BeginForm:
@using (Html.BeginForm("ActionName","ControllerName", FormMethod.Post, new { id = "filterform" })){
回答by Albert Oclarit
how about putting a hidden input
如何放置一个隐藏的输入
<form method="post" action="/Lot/LotList?auctionEventId=@auctionEventId" id="filterForm">
<input type="hidden" id="showAll" name="showAll" value=""/>
on your button
在你的按钮上
<input type="button" onclick="submitForm()" value="Show all" />
on your script somewhere
在你的脚本某处
function submitForm(){
$('#showAll').val('true');
$('#filterForm').submit();
}
回答by Brandon
If you are within a form you can add the route to a button element. You can override the native action of the form by using formAction and formMethod. You can also have multiple buttons with other formActions and formMethods
如果您在表单中,则可以将路由添加到按钮元素。您可以使用 formAction 和 formMethod 覆盖表单的本机操作。你也可以有多个带有其他 formActions 和 formMethods 的按钮
<form action="/somewhere" method="get">
<input type="type" name="name" value=" " />
<button formaction="/Lot/LotList?auctionEventId=@auctionEventId&showAll=true" formmethod="post">Show All</button>
</form>
回答by Matthias McCarthy
Hello I found this to work very well
你好,我发现这很好用
@using (Html.BeginForm("ActionName","ControllerName", new { id = "filterform" },FormMethod.Post)){
I had tired to do the 'FormMethod.Post' before the param but it didn't work and never passed the value. Only through trial and error did I manage to solve it and figured out that it should be like the code I posted.
我厌倦了在参数之前执行“FormMethod.Post”,但它不起作用并且从未传递过该值。只有通过反复试验,我才设法解决它并发现它应该像我发布的代码一样。
Hope that helps
希望有帮助
回答by Mohamed Nagieb
If you have multiple submit buttons for the same form, and you need to pass a different parameter for each of them, you can use the asp-route attribute to accomplish this task.
如果同一个表单有多个提交按钮,并且需要为每个按钮传递不同的参数,则可以使用 asp-route 属性来完成此任务。
<input type="submit" value="Save"/>
<input id="btnComplete" type="submit"
asp-route-addNewOneAfterSave="@true"
value="Save And Create New One">
Just remember to have your parameter in the OnPost method, for examples:
请记住在 OnPost 方法中包含您的参数,例如:
public async Task<IActionResult> OnPostAsync(bool? addNewOneAfterSave)
{
//Your code ....
}
You can read more about the asp-route anchor tag helper from this article.

