添加其他参数以表单提交

Sac*_*nth 20 forms asp.net-mvc razor

我在Razor视图中有一个表单声明

<form method="post" action="/Lot/LotList?auctionEventId=@auctionEventId" id="filterForm">
Run Code Online (Sandbox Code Playgroud)

(顺便说一句,我选择像这样写出来而不是使用Html.BeginFrom因为我需要给它一个id而不知道怎么做Html.BeginFrom- 但这不是问题所在)

在此表单之外,我有一个提交此表单的按钮(表单中还有一个提交按钮)

<input type="button" onclick="$('#filterForm').submit();" value="Show all" />
Run Code Online (Sandbox Code Playgroud)

现在,问题是如果使用此按钮提交表单,我希望表单中的操作更改为

action="/Lot/LotList?auctionEventId=@auctionEventId&showAll=true"
Run Code Online (Sandbox Code Playgroud)

如何更改操作并传递此附加参数?有没有更好的方法来做这一切?

Jam*_*iec 38

将查询字符串参数附加到表单操作上,并尝试在运行时更改它是棘手的(但并非不可能).使用隐藏字段要容易得多:

<form method="post" action="/Lot/LotList" id="filterForm">
  <input type="hidden" name="auctionEventId" value="@auctionEventId" />
  ...
Run Code Online (Sandbox Code Playgroud)

所以你现在所要​​做的就是为"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" />
  ...
Run Code Online (Sandbox Code Playgroud)

然后在showAll按钮上挂钩jquery事件:

<input id="showAllButton" type="button"/>
Run Code Online (Sandbox Code Playgroud)

jQuery的:

$('#showAllButton').click(function(){
     $('#showAllField').val("true");
     $('#filterForm').submit();
});
Run Code Online (Sandbox Code Playgroud)


Alb*_*rit 5

把隐藏的输入怎么样

<form method="post" action="/Lot/LotList?auctionEventId=@auctionEventId" id="filterForm">

<input type="hidden" id="showAll" name="showAll" value=""/>
Run Code Online (Sandbox Code Playgroud)

在你的按钮上

<input type="button" onclick="submitForm()" value="Show all" />
Run Code Online (Sandbox Code Playgroud)

在你的脚本某处

function submitForm(){
$('#showAll').val('true');
$('#filterForm').submit();

}
Run Code Online (Sandbox Code Playgroud)


yar*_*lty 5

我知道你说这不是问题,但你可以添加这样的属性Html.BeginForm:

 @using (Html.BeginForm("ActionName","ControllerName", FormMethod.Post, new { id = "filterform" })){
Run Code Online (Sandbox Code Playgroud)