我的 Razor View 中有一个表单声明
<form method="post" action="/Lot/LotList?auctionEventId=@auctionEventId" id="filterForm">
(顺便说一句,我选择这样写而不是使用
Html.BeginFrom
因为我需要给它一个 id 并且不知道如何用 Html.BeginFrom
来做到这一点 - 但这不是这里的问题)在此表单之外,我有一个提交此表单的按钮(表单中还有一个提交按钮)
<input type="button" onclick="$('#filterForm').submit();" value="Show all" />
现在,问题是,如果使用此按钮提交表单,我希望表单中的操作更改为
action="/Lot/LotList?auctionEventId=@auctionEventId&showAll=true"
如何更改操作并传递此附加参数?有没有更好的方法来做这一切?
最佳答案
将查询字符串参数附加到表单操作上,并尝试在运行时更改它是很棘手的(但并非不可能)。使用隐藏字段要容易得多:
<form method="post" action="/Lot/LotList" id="filterForm">
<input type="hidden" name="auctionEventId" value="@auctionEventId" />
...
所以现在您要做的就是为“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" />
...
只需在 showAll 按钮上连接一个 jquery 事件:
<input id="showAllButton" type="button"/>
jQuery:
$('#showAllButton').click(function(){
$('#showAllField').val("true");
$('#filterForm').submit();
});
关于asp.net-mvc - 向表单提交添加附加参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13162819/